我正在尝试使用2 var作为参数来发出http发布请求。如果我在文本字段中键入第一个和第二个var,然后单击Submit,它将执行http请求。它可以工作,但是我正在尝试将响应重新格式化/解析为List。
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'),
],
),
);
}
}
和
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,
};
}
现在,我显示此(图像),并且希望显示响应,如列表。