![](/img/trans.png)
[英]Flutter/Dart, cannot set a variable to a value from a json asset
[英]Dart / Flutter - cannot assign bool value from JSON response
Dart/Flutter 新手在從 JSON 響應分配 bool 值時遇到問題 - bool error
為null
,我得到:
失敗的斷言:布爾表達式不能為空
我不知道發生了什么,因為響應被正確解碼並且其他字段沒有問題(請查看 Logcat 輸出)。
這是我的 JSON:
{
"error:":false,
"id":1,
"name":"test"
}
我的未來:
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 exception e");
}
和 Logcat 輸出:
I/flutter: {
I/flutter: "error:":false,
I/flutter: "id":1,
I/flutter: "name":"test"
I/flutter: }
I/flutter: bool:null
I/flutter: id:1
I/flutter: name:test
I/flutter: Failed assertion: boolean expression must not be null
你能告訴我我在這里做錯了什么嗎? 任何幫助都感激不盡! 謝謝你 :)
我要感謝 Günter Zöchbauer 指出我在 JSON 結構中的愚蠢錯誤:
"error:":false
應該:
"error":false
不要忘記從編碼人員那里休息一下......;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.