【问题标题】:Flutter with Python Django RESTFUL APIFlutter 与 Python Django RESTFUL API
【发布时间】:2018-11-21 13:52:54
【问题描述】:

需要帮助

我已经为我的应用程序中的用户注册编写了一个颤振小部件,以及一个带有 Django RESTFUL API 的 API。如何将 python Django 中的 API 路由/URL 与颤振小部件连接或集成?请,我需要一个示例代码。我将不胜感激。

这是我的注册颤振小部件:

  import 'package:flutter/material.dart';

    class SignUpPage2 extends StatefulWidget {
      @override
     SignUpPage2State createState() => SignUpPage2State();
    }

    class SignUpPage2State extends State<SignUpPage2> { 

    @override
      Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,

        leading:  IconButton(
                  icon: new Icon(Icons.arrow_back, 
                  color:Colors.orange.shade700),
                        onPressed: () { 

                          Navigator.pop(context);

                           },
                    ),

        title: Text("Create acount", style: TextStyle(color:Colors.orange.shade700)),
        backgroundColor: Colors.black,
      ),
      backgroundColor: Colors.black45,
      body: Center(
        child: ListView(
          shrinkWrap: true,
          padding: EdgeInsets.only(left: 24.0, right: 24.0),
          children: <Widget>[

              new Center(
                child: new Text("Welcome",
                style: new TextStyle(
                  color: Colors.orange.shade700,
                  fontFamily: 'Poppins-Bold',
                  fontSize: 30.0,
                ),
                textAlign: TextAlign.center,
                ),   
            ), 
            SizedBox(height: 10.0),

            new Center(
                child: new Text("Please, Introduce Yourself",
                style: new TextStyle(
                  color: Colors.white,
                  fontFamily: 'Poppins',
                  fontSize: 20.0,
                ),
                textAlign: TextAlign.center,
                ),   
              ),  
            SizedBox(height: 20.0),

            TextField(
                keyboardType: TextInputType.text,
                autofocus: false,
                decoration: InputDecoration(
                  hintText: 'First Name',
                  filled: true,
                  fillColor: Colors.white,
                  contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(32.0),
                  ),
                ),
              ),
              SizedBox(height: 15.0),


              TextField(
                  keyboardType: TextInputType.text,
                  autofocus: false,
                  decoration: InputDecoration(
                    hintText: 'Last Name',
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(32.0),
                    ),
                  ),
                ),
              SizedBox(height: 15.0),

              TextField(
                  keyboardType: TextInputType.phone,
                  autofocus: false,
                  decoration: InputDecoration(
                    hintText: 'Phone',
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(32.0),
                    ),
                  ),
                ),
              SizedBox(height: 15.0),


               TextField(
                  keyboardType: TextInputType.datetime,
                  autofocus: false,

                  decoration: InputDecoration(
                    hintText: 'Date of Birth',
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(32.0),
                    ),
                  ),
                ),
              SizedBox(height: 15.0),

              TextField(
                    keyboardType: TextInputType.text,
                    autofocus: false,
                    obscureText: true,
                    // initialValue: 'john@gmail.com',
                    decoration: InputDecoration(
                      hintText: 'Password',
                      filled: true,
                      fillColor: Colors.white,
                      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(32.0),
                      ),
                    ),
                  ),
              SizedBox(height: 15.0),

              Padding(
                      padding: EdgeInsets.symmetric(
                        vertical: 16.0),
                        child: Material(
                          borderRadius: BorderRadius.circular(30.0),
                          // shadowColor: Colors.orange.shade700,
                          // elevation: 5.0,
                          child: MaterialButton(
                              minWidth: 200.0,
                              height: 60.0,
                              onPressed: (){
                                setState(() {

                                Navigator.of(context).pushNamed('/SignUpPage3');


                              } ,
                              color: Colors.orange.shade700,
                              child: Text(
                                "Next", 
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 23.0,
                                  ),
                              ),
                          ),
                        ),
                    ),

          ]
        ),
      ),
    );
  }}

【问题讨论】:

  • 你应该提供一个最小的例子来重现问题,这太长了

标签: python django dart flutter


【解决方案1】:

您需要将http 包添加到您的项目中。然后将对 Python REST 端点的 http 调用包装在 Future 中。 我讨厌发布链接作为答案,但在这种情况下,想不出比这本食谱更好的东西了 example 它完美地回答了您的问题!

【讨论】:

  • 我会试试这个。
【解决方案2】:

此外,当您运行 django 服务器时,请确保使用以下命令运行它: python manage.py runserver 0.0.0.0:8000 .

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 2021-08-09
    • 2012-03-01
    相关资源
    最近更新 更多