dart Error:List< dynamic>不是Map&lt;String,dynamic&gt;类型的子类型当插入json到框配置单元时

nkoocmlb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(141)

我目前正在构建一个应用程序。我已经收到了来自我调用的API的响应。只是当我想将响应保存到Hive框中时,出现了如下错误

我为UnitModel(unit_model.dart)创建了一个模型类:

// To parse this JSON data, do
//
//     final unitModel = unitModelFromJson(jsonString);

import 'dart:convert';

UnitModel unitModelFromJson(String str) => UnitModel.fromJson(json.decode(str));

String unitModelToJson(UnitModel data) => json.encode(data.toJson());

class UnitModel {
    bool status;
    String msg;
    List<UnitListDataModel> data;

    UnitModel({
        required this.status,
        required this.msg,
        required this.data,
    });

    factory UnitModel.fromJson(Map<String, dynamic> json) => UnitModel(
        status: json["status"],
        msg: json["msg"],
        data: List<UnitListDataModel>.from(json["data"].map((x) => UnitListDataModel.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "status": status,
        "msg": msg,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
    };
}

class UnitListDataModel {
    int? id;
    String? kodeUnit;
    double? endMeter;

    UnitListDataModel({
        this.id,
        this.kodeUnit,
        this.endMeter,
    });

    factory UnitListDataModel.fromJson(Map<String, dynamic> json) => UnitListDataModel(
        id: json["id"],
        kodeUnit: json["kode_unit"],
        endMeter: json["end_meter"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "kode_unit": kodeUnit,
        "end_meter": endMeter,
    };
}

字符串
这是meter_service.dart读取json

class MeterServices {
  final _api = DioHelper().getDio();
  final _prefs = Prefs();

  Future<void> _addItem(List<Map<String, dynamic>> newItems) async {
    for (var newItem in newItems) {
      await _prefs.item.add(newItem);
    }
  }

  Future<UnitListDataModel> getListUnit({String? kode}) async {
    final res = await _api.post('/unit/get');
    if (_prefs.item.isEmpty) {
      // print("______________________");
      // print(res.data);
      // print("______________________");

      _addItem(res.data['data']);
    }

    return UnitListDataModel.fromJson(res.data);
  }
}


我如何克服这个错误,使数据可以存储在Hive盒,我提供.谢谢

egmofgnx

egmofgnx1#

您可以通过将其编码为String来保存整个JsonResponse。当从HIVE中检索时,您可以使用jsonEncodejsonDecode将其解码回Map。

void main() {
  Map<String, dynamic> json = {
    "status": true,
    "msg": "Data Found",
    "data": [
      {
        "id": 1,
        "kode_unit": "qwerty",
        "end_meter": 12
      },
      {
        "id": 2,
        "kode_unit": "abcd",
        "end_meter": 3
      },
      {
        "id": 3,
        "kode_unit": "asdfg",
        "end_meter": 5
      }
    ]
  };
  
  String jsonEncoded = jsonEncode(json);//save this in HIVE as String
  
  print(jsonEncoded); 
  
  Map<String, dynamic> decodedMap = jsonDecode(jsonEncoded);// Decode it to Map when retrieving from HIVE
  
  print(decodedMap);
  
  List list = decodedMap['data'];
  
  List<UnitListDataModel> model = list.map((e) => UnitListDataModel.fromJson(e)).toList();
  
  for(var unit in model){
    print(unit.kodeUnit);
  }
}

字符串

相关问题