Home > OS >  Unhandled Exception: type '(dynamic) => Store' is not a subtype of type '(String,
Unhandled Exception: type '(dynamic) => Store' is not a subtype of type '(String,

Time:03-01

Unhandled Exception: type '(dynamic) => Store' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

The Json Data its on Object type How Can we Access the Datum and Return List of Datum Values

The Below Code is Model of Json Data

List<Store> storeFromJson(String str) =>
    List<Store>.from(json.decode(str).map((e) => Store.fromJson(e)));

String storeToJson(List<Store> data) =>
    json.encode(List<dynamic>.from(data.map((e) => e.toJson())));

class Store {
  Store({
    required this.success,
    required this.message,
    required this.code,
    required this.data,
  });

  bool success;
  String message;
  int code;
  List<Datum> data;

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        success: json["success"],
        message: json["message"],
        code: json["code"],
        data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
      );

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

class Datum {
  Datum({
    required this.store,
    required this.storeId,
    required this.storeType,
    required this.createdAt,
  });

  String store;
  int storeId;
  StoreType? storeType;
  int createdAt;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        store: json["store"],
        storeId: json["store_id"],
        storeType: storeTypeValues.map[json["store_type"]],
        createdAt: json["created_at"],
      );

  Map<String, dynamic> toJson() => {
        "store": store,
        "store_id": storeId,
        "store_type": storeTypeValues.reverse[storeType],
        "created_at": createdAt,
      };
}

CodePudding user response:

I assume that the data you are parsing is giving you a list of Store and every item from the list of store gives you list of Datum. To access data from sub list you can try this.

  final result = storeFromJson('data');
  List<Datum> storeList = result[0].data;

CodePudding user response:

I have edited your classes and toJson fromJson. Can you try this version of class

List<Store> storeFromJson(String str) =>
   (json.decode(str) as List<dynamic>).map((e) => Store.fromJson(e)).toList();

String storeToJson(List<Store> data) =>
    json.encode(data.map((e) => e.toJson()).toList());

class Store {
  Store({
    required this.success,
    required this.message,
    required this.code,
    required this.data,
  });

  bool success;
  String message;
  int code;
  List<Datum> data;

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        success: json["success"],
        message: json["message"],
        code: json["code"],
        data: (json["data"] as List<dynamic>).map((x) => Datum.fromJson(x as Map<String, dynamic>)),
      );

  Map<String, dynamic> toJson() => {
        "success": success,
        "message": message,
        "code": code,
        "data": data.map((x) => x.toJson().toList()),
      };
}

class Datum {
  Datum({
    required this.store,
    required this.storeId,
    required this.storeType,
    required this.createdAt,
  });

  String store;
  int storeId;
  StoreType? storeType;
  int createdAt;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        store: json["store"],
        storeId: json["store_id"],
        storeType: storeTypeValues.map[json["store_type"]],
        createdAt: json["created_at"],
      );

  Map<String, dynamic> toJson() => {
        "store": store,
        "store_id": storeId,
        "store_type": storeTypeValues.reverse[storeType],
        "created_at": createdAt,
      };
}

  • Related