簡體   English   中英

Flutter Web - 需要一個&#39;Map&#39;類型的值<String, dynamic> &#39;,但得到了“空”類型之一

[英]Flutter Web - Expected a value of type 'Map<String, dynamic>', but got one of type 'Null'

我在嘗試從 api 轉換響應時得到這個,響應正文不為空。 我確信這一點,因為我在轉換之前打印了正文。

需要一個“Map<String, dynamic>”類型的值,但得到一個“Null”類型的值

顫振版本 3.0.4

用於轉換響應正文的代碼:

 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");
    }
  }

報價模型類:

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);
}

回復正文:

{
    "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
        }
    ]
}

顫振醫生 -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

您可能在模型中將某些內容定義為不可為空,而在 JSON 示例中實際上為空。 例如, ForBatch.result

可能在您的模型中將其設為可空可以解決該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM