简体   繁体   English

扑动:输入'_InternalLinkedHashMap<object?, object?> ' 不是 'Map 类型的子类型<string, dynamic> '</string,></object?,>

[英]flutter: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>'

I get this error while serializing my json.序列化我的 json 时出现此错误。 I am not sure I understand the error, but I have figured out that the code never reaches Prices.fromJson if I try to print something there.我不确定我是否理解错误,但我发现如果我尝试在那里打印一些东西,代码永远不会到达 Prices.fromJson。 As you can see I am using Firebase Cloud Functions to return a data with nested data objects, and I struggle to get them serialized.如您所见,我正在使用 Firebase Cloud Functions 返回带有嵌套数据对象的数据,但我很难将它们序列化。

Code:代码:

class Price {
  final DateTime validFrom;
  final DateTime validTo;
  final double nokPerKwh;

  const Price({
    required this.validFrom,
    required this.validTo,
    required this.nokPerKwh,
  });

  factory Price.fromJson(Map<String, dynamic> json) {
    return Price(
      validFrom: DateTime.parse(json['validFrom']),
      validTo: DateTime.parse(json['validTo']),
      nokPerKwh: json['nokPerKwh'],
    );
  }
}

class Prices {
  final double now;
  final Price lowest;
  final Price highest;

  const Prices({
    required this.now,
    required this.lowest,
    required this.highest,
  });

  factory Prices.fromJson(Map<String, dynamic> json) {
    return Prices(
      now: json['now'],
      lowest: Price.fromJson(json['lowest']),
      highest: Price.fromJson(json['highest']),
    );
  }
}

class ShowerCost {
  final DateTime time;
  final int minutes;
  final Prices prices;

  const ShowerCost({
    required this.time,
    required this.minutes,
    required this.prices,
  });

  factory ShowerCost.fromJson(Map<String, dynamic> json) {
    print(json); <--- {minutes: 20, time: 2022-02-07T23:46:41.625Z, prices: {now: 11.848, highest: null, lowest: {nokPerKwh: 1.1848, validFrom: 2022-02-08T00:00:00+01:00, validTo: 2022-02-08T01:00:00+01:00}}}
    return ShowerCost(
      time: DateTime.parse(json['time']),
      minutes: json['minutes'],
      prices: Prices.fromJson(json['prices']),
    );
  }
}

Future<ShowerCost> getShowerCost() async {
  try {
    HttpsCallable callable =
        FirebaseFunctions.instance.httpsCallable('getShowerCost');
    final results = await callable.call(<String, dynamic>{
      'minutes': 20,
      'time': DateTime.now().toIso8601String(),
      'minHour': DateTime(2022, 2, 7, 0).toIso8601String(),
      'maxHour': DateTime(2022, 2, 7, 23).toIso8601String()
    });
    return ShowerCost.fromJson(results.data);
  } catch (error) {
    print(error);
    return Future.error(error);
  }
}

You can use json.decode from dart:convert package.您可以使用dart:convert包中的json.decode

    final results = await callable.call(<String, dynamic>{
      'minutes': 20,
      'time': DateTime.now().toIso8601String(),
      'minHour': DateTime(2022, 2, 7, 0).toIso8601String(),
      'maxHour': DateTime(2022, 2, 7, 23).toIso8601String()
    });
    // Try
    final data = Map<String, dynamic>.from(results.data) as Map<String, dynamic>
    // or
    final data = results.data.toMap()

    return ShowerCost.fromJson(data);

Both @rapaterno's and @mohamed abu-ghazalla's answers pointed me into the right direction. @rapaterno 和@mohamed abu-ghazalla 的回答都为我指明了正确的方向。 Conversion to Map<String, dynamic> using Map<String, dynamic>.from(...) needed to happen in all the child's fromJson() params:使用Map<String, dynamic>.from(...)转换为Map<String, dynamic>需要在所有孩子的fromJson()参数中发生:

factory Price.fromJson(Map<String, dynamic> json) {
  return Price(
    validFrom: DateTime.parse(json['validFrom']),
    validTo: DateTime.parse(json['validTo']),
    nokPerKwh: json['nokPerKwh'],
  );
}

factory Prices.fromJson(Map<String, dynamic> json) {
  return Prices(
    now: json['now'],
    lowest: Price.fromJson(Map<String, dynamic>.from(json['lowest'])),   <--- HERE
    highest: Price.fromJson(Map<String, dynamic>.from(json['highest'])), <--- HERE
  );
}

factory ShowerCost.fromJson(Map<String, dynamic> json) {
  return ShowerCost(
    time: DateTime.parse(json['time']),
    minutes: json['minutes'],
    prices: Prices.fromJson(Map<String, dynamic>.from(json['prices'])),  <--- HERE
  );
}

Future<ShowerCost> getShowerCost() async {
  try {
    HttpsCallable callable =
        FirebaseFunctions.instance.httpsCallable('getShowerCost');
    final results = await callable.call(<String, dynamic>{
      'minutes': 20,
      'time': DateTime.now().toIso8601String(),
      'minHour': DateTime(2022, 2, 8, 0).toIso8601String(),
      'maxHour': DateTime(2022, 2, 8, 23).toIso8601String()
    });
    return ShowerCost.fromJson(results.data);
  } catch (error) {
    print(error);
    return Future.error(error);
  }
}

use this:用这个:

final myMap = Map<String, dynamic>.from(results.data);
return Prices.fromJson(myMap);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter StreamBuilder小部件错误:类型'()=&gt; Map<string, dynamic> ' 不是类型 'DocumentSnapshot 的子类型<object?> ' 在类型转换中</object?></string,> - Flutter StreamBuilder widget Error : type ' () => Map<String, dynamic >' is not a subtype of type 'DocumentSnapshot<Object?>' in type cast 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> ? 在类型转换中</dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>?' in type cast 例外:“字符串”类型不是“地图”类型的子类型<dynamic, dynamic> ' 在 flutter 的类型中</dynamic,> - Exception: type 'String' is not a subtype of type 'Map<dynamic, dynamic>' in type cast in flutter 输入'列表<map<string, dynamic> &gt;' 不是 'Map 类型的子类型<dynamic, dynamic> ' </dynamic,></map<string,> - type 'List<Map<String, dynamic>>' is not a subtype of type 'Map<dynamic, dynamic>' Flutter:如何解决值类型 Object 无法分配给 Map<string, dynamic> 在流构建器上</string,> - Flutter: How to solve value type Object can't be assigned to Map<String, dynamic> on streambuilder Flutter:“DateTime”类型不是“String”类型的子类型 - Flutter: type 'DateTime' is not a subtype of type 'String' 输入“列表<dynamic> ' 不是类型 'String' 的子类型</dynamic> - type 'List<dynamic>' is not a subtype of type 'String' 输入'未来<dynamic> ' 不是类型 'Future 的子类型<string> '</string></dynamic> - type 'Future<dynamic>' is not a subtype of type 'Future<String>' 颤振类型'未来<dynamic> ' 不是类型 'Widget' 错误的子类型</dynamic> - Flutter type 'Future<dynamic>' is not a subtype of type 'Widget' error 获取存储 | _CastError(“字符串”类型不是“列表”类型的子类型<dynamic> ? 在类型转换中)</dynamic> - GetStorage | _CastError (type 'String' is not a subtype of type 'List<dynamic>?' in type cast)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM