小编Mat*_*att的帖子

Dart / Flutter - 无法从 JSON 响应分配 bool 值

Dart/Flutter 新手在从 JSON 响应分配 bool 值时遇到问题,我得到bool errornull

断言失败:布尔表达式不能为空

我不知道发生了什么,因为响应已正确解码,并且其他字段没有问题(请查看 Logcat 输出)。

这是我的 JSON:

{
"error:":false,
"id":1,
"name":"test"
}
Run Code Online (Sandbox Code Playgroud)

我的未来:

Future<dynamic> fetchData() async {
http.Response response = await http.get(Values.URL, headers: {HttpHeaders.contentTypeHeader: "application/json"});

if (response.statusCode == 200) {
  debugPrint(response.body);

  var body = jsonDecode(response.body);

  bool error = body["error"];
  var id = body["id"];
  var name = body["name"];

  print("bool:" + error.toString());
  print("id:" + id.toString());
  print("name:" + name);

  if (error) {
    print("no error");
  } else {
    print("error");
  }
} else {
  throw Exception("statusCode …
Run Code Online (Sandbox Code Playgroud)

json boolean dart flutter

6
推荐指数
1
解决办法
5663
查看次数

标签 统计

boolean ×1

dart ×1

flutter ×1

json ×1