json - Flutter - The method '[]' was called on null
问题描述
I' just retreiving the profile information from the server, i ain't creating any model I'm directly updating the values but while fetching the data I'm getting an error as Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
This is my json response
{
"status": true,
"record": {
"user_id": "20",
"user_name": null,
"user_phone": null,
"user_email": null
}
}
And this is how i'm fetching from server and saving it.
String userId = "";
final response = await http.post(profileUrl, headers: headers, body: body);
if(response.statusCode == 200){
print('Response in Profile Screen: ' + response.body);
setState(() {
username = jsonResponse['record']['user_id];
print(username + "Userid");
});
}
And when I'm trying to print the userId i'm getting the error
解决方案
您应该先将response.body分配给jsonResponse
var jsonResponse = response.body;
jsonResponse['record']['user_id'];
推荐阅读
- node.js - 如何使用来自 GCP Firestore 的大量数据在 Vue.js 数据表中工作
- r - 无法在 R 版本 4.1.0 上使用 timetk 将双向量转换为函数
- javascript - 这可以成为 Javascript 中高阶组件的示例吗?
- php - 如何从复选框创建一个数组
- ios - Flutter 音频播放器声音延迟
- ios - UINavigationController 过渡到 defaultBackground 显示白色边框
- salesforce - Salesforce 闪电文件上传
- c# - 日期时间的人性化器
- asp.net-core - 如何更改 asp.netcore 3.1 的默认登录路径
- node.js - TypeError:getDb 不是具有额外解构问题的函数