我正在嘗試從我的 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(), ); }
1 回答

冉冉說
TA貢獻1877條經驗 獲得超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}");
}
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消