我一直在下面与这个问题作斗争。我正在从api中获取数据,如下所示:
{
"type": 1,
"message": "Login successful!",
"data": {
"id": 22,
"name": "MIke Man",
"email": "[email protected]",
"email_verified_at": "2020-02-01 15:04:12",
"username": "mikeman22",
"phonenumber": null,
"profile_picture": "default_user.png",
"cover_picture": "default_cover.jpg",
"is_verified": null,
"bio": "Hi, I'm a Software Developer, you can contact me here to build amazinf apps.",
"links": null,
"pinned_post": "0",
"role_id": "1",
"is_online": null,
"tfa_key": "TBKFCFYBH2MS7YMP",
"tfa_active": "0",
"created_at": null,
"updated_at": null
}
}
如何使Flutter理解此类数据,以下是我的代码:
Future<dynamic> sendData(url, data) async {
http.Response res = await CallApi().postData(data, url);
var body = json.decode(res.body);
return body;
}
Future<dynamic> res = await repo.sendData('dfds/dsfds/sdfds', data);
res.then((body) {
});
请注意,我遇到过这样的问题,但似乎没有帮助,因为他们的数据与我的数据不一样。
Flutter了解您的数据类型,据我所知,您正在等待一个方法,然后等待另一个方法。await前缀需要动态数据。因此,每次您以动态方式堆叠动态方式时。如果您能理解我的解释。但是简单地删除repo.sendData()
之前的await像这样:
Future<dynamic> res = repo.sendData('dfds/dsfds/sdfds', data);
res.then((body) {
});