Newbie to Dart/Flutter here struggling with problem while assigning bool valuse from JSON response - bool error is null and I'm getting:

Failed assertion: boolean expression must not be null

I don't know what's going on as the response is decoded properly and there's no problem with other fields (please take look at Logcat output).

this is my JSON:


my Future:

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

if (response.statusCode == 200) {

  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 {
} else {
  throw Exception("statusCode exception e");

and the Logcat output:

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

Can you please let me know what I am doing wrong here? Any help will be much appreciated! THANK YOU :)

Solution 1: Matt

I'd like to thank Günter Zöchbauer for pointing out my silly mistake in JSON structure:


should be:


don't forget to take a break from coding guys... ;)