firebase JSON和serialization _TypeError(类型'List< dynamic>'不是类型'List '的子类型< String>)

91zkwejq  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(154)

我试过删除一个列表字符串和一个双精度型的成员,它起作用了。它必须在Map中固定为一个列表和一个双精度型,但我不知道如何固定它。

我是新手,所以不知道如何修复JSON和序列化。

_TypeError(类型“动态列表"不是类型”列表字符串“的子类型)
部件“类列表食品,例如 dart ”;

part of 'class_list_food.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

MyCard _$MyCardFromJson(Map<String, dynamic> json) => MyCard(
      id: json['id'] as String,
      foodname: json['foodname'] as String,
      foodtype: json['foodtype'] as String,
      namepro: json['namepro'] as String,
      picpro: json['picpro'] as String,
      time: json['time'] as String,
      favorite: json['favorite'] as String,
      dianamepro: json['dianamepro'] as String,
      diapicpro: json['diapicpro'] as String,
      diatime: json['diatime'] as String,
      note: json['note'] as String,
      ingredient: json['ingredient'].map((string) => string.toString()).toList(),
      quantity:
          json['quantity'].map((string) => string.toString()).toList(),
      uom: json['uom'].map((string) => string.toString()).toList(),
      step: json['step'].map((string) => string.toString()).toList(),
      pic: json['pic'].map((string) => string.toString()).toList(),
      dianote:
          json['dianote'].map((string) => string.toString()).toList(),
      region: json['region'] as String,
      recipetype: json['recipetype'] as String,
      desc: json['desc'] as String,
      flav: json['flav'] as String,
      rating: (json['rating'] as num).toDouble(),
    );

Map<String, dynamic> _$MyCardToJson(MyCard instance) => <String, dynamic>{
      'id': instance.id,
      'foodname': instance.foodname,
      'foodtype': instance.foodtype,
      'namepro': instance.namepro,
      'picpro': instance.picpro,
      'time': instance.time,
      'favorite': instance.favorite,
      'dianamepro': instance.dianamepro,
      'diapicpro': instance.diapicpro,
      'diatime': instance.diatime,
      'note': instance.note,
      'desc': instance.desc,
      'flav': instance.flav,
      'region': instance.region,
      'recipetype': instance.recipetype,
      'ingredient': instance.ingredient,
      'quantity': instance.quantity,
      'uom': instance.uom,
      'step': instance.step,
      'pic': instance.pic,
      'dianote': instance.dianote,
      'rating': instance.rating,
    };

Food _$FoodFromJson(Map<String, dynamic> json) => Food(
      id: json['id'] as String,
      foodname: json['foodname'] as String,
      foodtype: json['foodtype'] as String,
      namepro: json['namepro'] as String,
      picpro: json['picpro'] as String,
      time: json['time'] as String,
      favorite: json['favorite'] as String,
      dianamepro: json['dianamepro'] as String,
      diapicpro: json['diapicpro'] as String,
      diatime: json['diatime'] as String,
      note: json['note'] as String,
      ingredient: json['ingredient'].map((string) => string.toString()).toList(),
      quantity:
          json['quantity'].map((string) => string.toString()).toList(),
      uom: json['uom'].map((string) => string.toString()).toList(),
      step: json['step'].map((string) => string.toString()).toList(),
      pic: json['pic'].map((string) => string.toString()).toList(),
      dianote:
          json['dianote'].map((string) => string.toString()).toList(),
      region: json['region'] as String,
      recipetype: json['recipetype'] as String,
      desc: json['desc'] as String,
      flav: json['flav'] as String,
      rating: (json['rating'] as num).toDouble(),
    );

Map<String, dynamic> _$FoodToJson(Food instance) => <String, dynamic>{
      'id': instance.id,
      'foodname': instance.foodname,
      'foodtype': instance.foodtype,
      'namepro': instance.namepro,
      'picpro': instance.picpro,
      'time': instance.time,
      'favorite': instance.favorite,
      'dianamepro': instance.dianamepro,
      'diapicpro': instance.diapicpro,
      'diatime': instance.diatime,
      'note': instance.note,
      'desc': instance.desc,
      'flav': instance.flav,
      'region': instance.region,
      'recipetype': instance.recipetype,
      'ingredient': instance.ingredient,
      'quantity': instance.quantity,
      'uom': instance.uom,
      'step': instance.step,
      'pic': instance.pic,
      'dianote': instance.dianote,
      'rating': instance.rating,
    };

/*Beverage _$BeverageFromJson(Map<String, dynamic> json) => Beverage(
      id: json['id'] as String,
      foodname: json['foodname'] as String,
      foodtype: json['foodtype'] as String,
      namepro: json['namepro'] as String,
      picpro: json['picpro'] as String,
      time: json['time'] as String,
      favorite: json['favorite'] as String,
      dianamepro: json['dianamepro'] as String,
      diapicpro: json['diapicpro'] as String,
      diatime: json['diatime'] as String,
      note: json['note'] as String,
      ingredient: (json['ingredient'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
      quantity:
          (json['quantity'] as List<dynamic>).map((e) => e as String).toList(),
      uom: (json['uom'] as List<dynamic>).map((e) => e as String).toList(),
      step: (json['step'] as List<dynamic>).map((e) => e as String).toList(),
      pic: (json['pic'] as List<dynamic>).map((e) => e as String).toList(),
      dianote:
          (json['dianote'] as List<dynamic>).map((e) => e as String).toList(),
      region: json['region'] as String,
      recipetype: json['recipetype'] as String,
      desc: json['desc'] as String,
      flav: json['flav'] as String,
      rating: (json['rating'] as num).toDouble(),
    );

Map<String, dynamic> _$BeverageToJson(Beverage instance) => <String, dynamic>{
      'id': instance.id,
      'foodname': instance.foodname,
      'foodtype': instance.foodtype,
      'namepro': instance.namepro,
      'picpro': instance.picpro,
      'time': instance.time,
      'favorite': instance.favorite,
      'dianamepro': instance.dianamepro,
      'diapicpro': instance.diapicpro,
      'diatime': instance.diatime,
      'note': instance.note,
      'desc': instance.desc,
      'flav': instance.flav,
      'region': instance.region,
      'recipetype': instance.recipetype,
      'ingredient': instance.ingredient,
      'quantity': instance.quantity,
      'uom': instance.uom,
      'step': instance.step,
      'pic': instance.pic,
      'dianote': instance.dianote,
      'rating': instance.rating,
    };
*/
zbdgwd5y

zbdgwd5y1#

如果您要使用手动代码实现此功能。
这通常发生在发生强制转换问题时,主要是在FromJson()方法内部。
有几种方法可以解决这个问题,假设存在一个变量List<String> stringList;,json键为stringList

stringList = List<String>.from(json['stringList']);

或者

stringList = json['stringList'].map((string) => string.toString()).toList();

或者

stringList= List<String>.of(json['stringList']);

相关问题