Flutter 使用TextField控制器发送HTTP POST请求

Flutter 使用TextField控制器发送HTTP POST请求,flutter,Flutter,我正在尝试使用2var作为参数发出http post请求。如果我键入第一个和第二个var(在我的文本字段中),然后单击Submit,它将执行http请求。它可以工作,但我正在尝试将响应重新格式化/解析为列表 Future<Repair> getRepair(String Id, String title) async { final String apiUrl = "*****"; final response = await h

我正在尝试使用2var作为参数发出http post请求。如果我键入第一个和第二个var(在我的文本字段中),然后单击Submit,它将执行http请求。它可以工作,但我正在尝试将响应重新格式化/解析为列表

Future<Repair> getRepair(String Id, String title) async {
  final String apiUrl =
      "*****";

  final response =
      await http.post(apiUrl, body: {"id": Id, "title": title});

  if (response.statusCode == 200) {
    final String responseString = response.body;

    return repairModelFromJson(responseString);

  } else {
    print(null);
    return null;
  }
}

class _MainFetchDataState extends State<MainFetchData> {
  Repair _repair;

  final TextEditingController caseController = TextEditingController();
  final TextEditingController serialController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Fecth"),
      ),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.all(8.0),
        child: RaisedButton(
          child: Text("Submit"),
          onPressed: () async {
            final String Id = idController.text;
            final String title = titleController.text;

            final Repair repair = await getRepair(Id, title);

            setState(() {
              _repair = repair;
            });
          },
        ),
      ),
      body: Column(
        children: <Widget>[
          TextField(
            controller: idController,
          ),
          TextField(
            controller: titleController,
          ),
          SizedBox(
            height: 32,
          ),
          _repair == null
              ? Container()
              : Text(_repair.movement.toString() != null ? "${_repair.movement.toString()}" : 'Vuoto'),
        ],
      ),
    );
  }
}
Future getRepair(字符串Id、字符串标题)异步{
最终字符串apirl=
"*****";
最后答复=
等待http.post(apirl,正文:{“id”:id,“title”:title});
如果(response.statusCode==200){
最终字符串responseString=response.body;
返回repairModelFromJson(responseString);
}否则{
打印(空);
返回null;
}
}
类_MainFetchDataState扩展状态{
修理(u)修理;;
final TextEditingController caseController=TextEditingController();
final TextEditingController serialController=TextEditingController();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“Fecth”),
),
底部导航栏:填充(
填充:常数边集全部(8.0),
孩子:升起按钮(
儿童:文本(“提交”),
onPressed:()异步{
最终字符串Id=idController.text;
最终字符串标题=titleController.text;
最终维修=等待获得维修(Id,标题);
设置状态(){
_修理=修理;
});
},
),
),
正文:专栏(
儿童:[
文本字段(
控制器:idController,
),
文本字段(
控制器:标题控制器,
),
大小盒子(
身高:32,
),
_修复==null
?容器()
:Text(_repair.movement.toString()!=null?”${{u repair.movement.toString()}):“Vuoto”),
],
),
);
}
}

导入'dart:convert';
Repair repairModelFromJson(String str)=>Repair.fromJson(json.decode(str));
字符串repairModelToJson(修复数据)=>json.encode(data.toJson());
等级修理{
字符串Id;
字符串标题;
弦运动;
修理({
这个,身份证,,
这个名字,
这个运动,,
});
factory Repair.fromJson(映射json)=>Repair(
Id:json[“Id”],
标题:json[“标题”],
移动:json['movement'].toString(),
);
映射到JSON()=>{
“id”:id,
“头衔”:头衔,
“运动”:运动,
};
}
现在,我展示这个(图像),我想展示一个列表一样的响应


Hey leon您应该为此使用列表视图生成器Hey leon您应该为此使用列表视图生成器
import 'dart:convert';

Repair repairModelFromJson(String str) => Repair.fromJson(json.decode(str));

String repairModelToJson(Repair data) => json.encode(data.toJson());

class Repair {
  String Id;
  String title;
  String movement;

  Repair({
    this.Id,
    this.title,
    this.movement,
  });

  factory Repair.fromJson(Map<String, dynamic> json) => Repair(
    Id: json["id"],
    title: json["title"],
    movement: json['movement'].toString(),
  );

  Map<String, dynamic> toJson() => {
    "id": Id,
    "title": title,
    "movement": movement,
  };
}