FLUTTER&PHP - 我无法从我的 flutter 应用程序发布到 mysql 数据库

我正在尝试从我的 flutter 应用程序连接数据库,并且我想将我在文本字段中写入的值发布到数据库。我写了一些代码,但无法发布到数据库,它给了我错误。我想我必须编辑我的 php 代码,但我不知道如何编辑,请帮助我...下面的代码和错误


     Future<List> sendData() async {

   await http.post(

      "https://www.ekspar.com/trying/go.php",

      headers: {

        'Content-Type': 'application/json; charset=UTF-8',

      },

      body: {

        "adi": nameController.text,

        "soyadi": surnameController.text,

      },

    );

    

    json.decode(response.body);

  }


@override

  void initState() {

    sendData();

  }


@override

  Widget build(BuildContext context) {

    return Scaffold(

        backgroundColor: Theme.of(context).backgroundColor,

        body: Scaffold(

          appBar: AppBar(

            title: Text("Register"),

          ),

          body: Container(

            child: Center(

              child: Column(

                children: <Widget>[

                  Text(

                    "ad",

                    style: TextStyle(fontSize: 18.0),

                  ),

                  TextField(

                    controller: nameController,

                    decoration: InputDecoration(hintText: 'ad'),

                  ),

                  Text(

                    "soyad",

                    style: TextStyle(fontSize: 18.0),

                  ),

                  TextField(

                    controller: surnameController,

                    decoration: InputDecoration(hintText: 'soyad'),

                  ),

                  RaisedButton(

                    child: Text("Register"),

                    onPressed: () {

                      setState(() {

                        _build();

                      });

                      sendData();

                    },

                  ),

                  _build()

                ],

              ),

            ),

          ),

        )


        //(_buildBody(),

        );

  }


DIEA
浏览 128回答 1
1回答

冉冉说

看看你的 API 让我觉得你正在尝试发布数据。同时,我可以看到您在 flutter 应用程序中使用了 get 请求。如果您尝试发布数据,请从 flutter 应用程序发出 POST 请求,而不是 GET 请求。下面是flutter中使用HTTP包进行POST请求的示例。POST 请求示例:String url = "https://www.ekspar.com.tr/onarim/post.php";var response = await http.post(url, body: {    "adi":"YOUR_DATA",    "soyadi":"YOUR_DATA"});var body = jsonDecode(response.body);if(response.statusCode == 200){    debugPrint("Data posted successfully");}else{    debugPrint("Something went wrong! Status Code is: ${response.statusCode}");}
打开App,查看更多内容
随时随地看视频慕课网APP