Flutter Web – Expected a value of type 'Map<String, dynamic>', but got one of type 'Null'

Issue

I getting this while trying to convert the response from an api, the response body is not null. I am sure of it because I am printing the body before converting.

Expected a value of type ‘Map<String, dynamic>’, but got one of type ‘Null’

Flutter version 3.0.4

Code used to convert the response body:

 getRequestedQuoteList() async {
    var path = 'enquiryForQuotesList/';

    var responseData = await DioHandler.dioPost(path);
    if (responseData != null) {
      if (responseData['status'] == "success") {
        log(responseData.toString());
        await Future.delayed(Duration(seconds: 3));
        quoteModel = QuoteModel.fromJson(responseData);
        isLoading = false;
        update();
      }
    } else {
      log("Null");
    }
  }

QuoteModel class:

import 'package:freezed_annotation/freezed_annotation.dart';
import 'dart:convert';

part 'quote_model.freezed.dart';
part 'quote_model.g.dart';

QuoteModel quoteModelFromJson(String str) =>
    QuoteModel.fromJson(json.decode(str));

String quoteModelToJson(QuoteModel data) => json.encode(data.toJson());

@freezed
class QuoteModel with _$QuoteModel {
  const factory QuoteModel({
    required String status,
    required List<Result> result,
  }) = _QuoteModel;

  factory QuoteModel.fromJson(Map<String, dynamic> json) =>
      _$QuoteModelFromJson(json);
}

@freezed
class Result with _$Result {
  const factory Result({
    required int id,
    required FromBuyer fromBuyer,
    String? specification,
    required ForBatch forBatch,
    required bool responded,
    required double quantity,
    dynamic respondedBy,
  }) = _Result;

  factory Result.fromJson(Map<String, dynamic> json) => _$ResultFromJson(json);
}

@freezed
class ForBatch with _$ForBatch {
  const factory ForBatch({
    required int id,
    required String name,
    required String state,
    required String district,
    required String village,
    required int quantity,
    required DateTime dateOfHarvest,
    required double latitude,
    required double longitude,
    required DateTime timestamp,
    dynamic farmer,
    required int raithaSahayak,
    required Map<String, double> result,
  }) = _ForBatch;

  factory ForBatch.fromJson(Map<String, dynamic> json) =>
      _$ForBatchFromJson(json);
}

@freezed
class FromBuyer with _$FromBuyer {
  const factory FromBuyer({
    required int id,
    required String username,
    required String mobile,
    String? address,
  }) = _FromBuyer;

  factory FromBuyer.fromJson(Map<String, dynamic> json) =>
      _$FromBuyerFromJson(json);
}

Response body:

{
    "status": "success",
    "result": [
        {
            "id": 1,
            "fromBuyer": {
                "id": 3,
                "username": "srk",
                "mobile": "1",
                "address": null
            },
            "specification": "test",
            "forBatch": {
                "id": 34,
                "name": "IndirCDsreeh35108",
                "state": "Karnataka",
                "district": "Bangalore Urban",
                "village": "Indiranagar",
                "quantity": 3000,
                "dateOfHarvest": "2022-02-08",
                "latitude": 77.6415813,
                "longitude": 77.6415813,
                "timestamp": "2022-02-18T08:08:03.538064Z",
                "farmer": 6,
                "raithaSahayak": 3,
                "result": {
                    "total": 40,
                    "good": 20,
                    "infested": 19,
                    "discolored": 0,
                    "fungal": 0,
                    "broken": 0,
                    "mixed": 0,
                    "foreignParticles": 0,
                    "goodPercent": 50.0,
                    "infestedPercent": 47.5,
                    "discoloredPercent": 0.0,
                    "fungalPercent": 0.0,
                    "brokenPercent": 0.0,
                    "mixedPercent": 0.0,
                    "foreignParticlesPercent": 0.0,
                    "batch": 34
                }
            },
            "responded": false,
            "quantity": 100.0,
            "respondedBy": null
        },
        {
            "id": 2,
            "fromBuyer": {
                "id": 3,
                "username": "srk",
                "mobile": "1",
                "address": null
            },
            "specification": "test",
            "forBatch": {
                "id": 27,
                "name": "IndirCDsreeh68704",
                "state": "Karnataka",
                "district": "Bangalore Urban",
                "village": "Indiranagartest",
                "quantity": 1500,
                "dateOfHarvest": "2022-02-09",
                "latitude": 77.6415988,
                "longitude": 77.6415988,
                "timestamp": "2022-02-09T12:20:30.419288Z",
                "farmer": null,
                "raithaSahayak": 3,
                "result": {
                    "total": 475,
                    "good": 350,
                    "infested": 37,
                    "discolored": 16,
                    "fungal": 7,
                    "broken": 41,
                    "mixed": 19,
                    "foreignParticles": 21,
                    "goodPercent": 73.6842105263158,
                    "infestedPercent": 7.7894736842105265,
                    "discoloredPercent": 0.0,
                    "fungalPercent": 1.4736842105263157,
                    "brokenPercent": 8.631578947368421,
                    "mixedPercent": 4.0,
                    "foreignParticlesPercent": 4.421052631578948,
                    "batch": 27
                }
            },
            "responded": false,
            "quantity": 200.0,
            "respondedBy": null
        },
        {
            "id": 3,
            "fromBuyer": {
                "id": 3,
                "username": "srk",
                "mobile": "1",
                "address": null
            },
            "specification": "test",
            "forBatch": {
                "id": 31,
                "name": "IndirCDsreeh21671",
                "state": "Karnataka",
                "district": "Bangalore Urban",
                "village": "Indiranagar",
                "quantity": 1500,
                "dateOfHarvest": "2022-02-18",
                "latitude": 77.6415778,
                "longitude": 77.6415778,
                "timestamp": "2022-02-18T07:51:56.082605Z",
                "farmer": 7,
                "raithaSahayak": 3,
                "result": {
                    "total": 74,
                    "good": 25,
                    "infested": 48,
                    "discolored": 0,
                    "fungal": 0,
                    "broken": 0,
                    "mixed": 0,
                    "foreignParticles": 0,
                    "goodPercent": 33.78378378378378,
                    "infestedPercent": 64.86486486486487,
                    "discoloredPercent": 0.0,
                    "fungalPercent": 0.0,
                    "brokenPercent": 0.0,
                    "mixedPercent": 0.0,
                    "foreignParticlesPercent": 0.0,
                    "batch": 31
                }
            },
            "responded": false,
            "quantity": 300.0,
            "respondedBy": null
        },
        {
            "id": 4,
            "fromBuyer": {
                "id": 3,
                "username": "srk",
                "mobile": "1",
                "address": null
            },
            "specification": "test",
            "forBatch": {
                "id": 45,
                "name": "IndirCDsreeh23015",
                "state": "Karnataka",
                "district": "Bangalore Urban",
                "village": "Indiranagar",
                "quantity": 1000,
                "dateOfHarvest": "2022-03-16",
                "latitude": 77.6415324,
                "longitude": 77.6415324,
                "timestamp": "2022-03-24T11:59:06.908914Z",
                "farmer": 11,
                "raithaSahayak": 3,
                "result": {
                    "total": 338,
                    "good": 297,
                    "infested": 41,
                    "discolored": 0,
                    "fungal": 0,
                    "broken": 0,
                    "mixed": 0,
                    "foreignParticles": 0,
                    "goodPercent": 87.8698224852071,
                    "infestedPercent": 12.1301775147929,
                    "discoloredPercent": 0.0,
                    "fungalPercent": 0.0,
                    "brokenPercent": 0.0,
                    "mixedPercent": 0.0,
                    "foreignParticlesPercent": 0.0,
                    "batch": 45
                }
            },
            "responded": false,
            "quantity": 640.0,
            "respondedBy": null
        },
        {
            "id": 5,
            "fromBuyer": {
                "id": 1,
                "username": "admin",
                "mobile": "2",
                "address": null
            },
            "specification": "A Grade Channadal",
            "forBatch": {
                "id": 2,
                "name": "ThirdBatch",
                "state": "Karnataka",
                "district": "Bangalore Rural",
                "village": "Anneshwara",
                "quantity": 1000,
                "dateOfHarvest": "2022-01-02",
                "latitude": 46.18,
                "longitude": 73.15,
                "timestamp": "2022-01-21T08:17:50.960403Z",
                "farmer": null,
                "raithaSahayak": 1,
                "result": null
            },
            "responded": false,
            "quantity": 500.0,
            "respondedBy": null
        },
        {
            "id": 6,
            "fromBuyer": {
                "id": 1,
                "username": "admin",
                "mobile": "2",
                "address": null
            },
            "specification": "A Grade Channadal",
            "forBatch": {
                "id": 2,
                "name": "ThirdBatch",
                "state": "Karnataka",
                "district": "Bangalore Rural",
                "village": "Anneshwara",
                "quantity": 1000,
                "dateOfHarvest": "2022-01-02",
                "latitude": 46.18,
                "longitude": 73.15,
                "timestamp": "2022-01-21T08:17:50.960403Z",
                "farmer": null,
                "raithaSahayak": 1,
                "result": null
            },
            "responded": false,
            "quantity": 500.0,
            "respondedBy": null
        }
    ]
}

Flutter doctor -v

[√] Flutter (Channel stable, 3.0.4, on Microsoft Windows [Version 10.0.19043.1586], locale en-IN)
    • Flutter version 3.0.4 at C:\Users\DELL\Documents\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 85684f9300 (7 days ago), 2022-06-30 13:22:47 -0700
    • Engine revision 6ba2af10bb
    • Dart version 2.17.5
    • DevTools version 2.12.2

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\DELL\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.69.0)
    • VS Code at C:\Users\DELL\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.44.0

[√] Connected device (4 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Windows (desktop)       • windows       • windows-x64    • Microsoft Windows [Version 10.0.19043.1586]
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 103.0.5060.114
    • Edge (web)              • edge          • web-javascript • Microsoft Edge 103.0.1264.49

[√] HTTP Host Availability
    • All required HTTP hosts are available

Solution

You might have some things defined in your model as non-nullable that are actually null in the JSON example. For instance, ForBatch.result.

Possibly making that nullable in your model could solve the issue.

Answered By – dumazy

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *