【问题标题】:parsing json into flutter List<dynamic> to Map<String, dynamic>将json解析成flutter List<dynamic> to Map<String, dynamic>
【发布时间】:2021-09-01 17:00:48
【问题描述】:

您好,我想解析 Json,我遇到了这个问题,请问有什么问题请帮助
错误是“List”不是“Map”类型的子类型
Model

class CategoriesModel {
  CategoriesModel({
    this.id,
    this.title,
    this.price,
    this.description,
    this.image,
  });

  int? id;
  String? title;
  double? price;
  String? description;
  String? image;

  factory CategoriesModel.fromJson(Map<String, dynamic> json) {
    return CategoriesModel(
      id: json["id"],
      title: json["title"],
      price: json["price"].toDouble(),
      description: json["description"],
      image: json["image"],
    );
  }


  Map<String, dynamic> toMap() => {
    "id": id,
    "title": title,
    "price": price,
    "description": description,
    "image": image,
  };
}

cubit 我做了这个 (AppCubit.dart)

CategoriesModel? categoriesModel ;

   getCategories(){
    emit(OnLoadingCategoriesState());
    DioHelper.getData(pathUrl: "products").then((value){
      categoriesModel = CategoriesModel.fromJson(value.data);
      // catList =  (value.data as List)
      //     .map((x) => CategoriesModel.fromJson(x))
      //     .toList();
      // print(catList.length);

      emit(OnSuccessCategoriesState());
    }).catchError((error){
      emit(OnCatchErrorCategoriesState(error.toString()));
      print(error.toString());
    });
  }

Dio Helper 我做了通用功能来使用它

  static Future<Response> getData({required pathUrl})async{
   return await dio.get(pathUrl);
  }

这是来自 json

的小数据
[
  {
    "id": 1,
    "title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
    "price": 109.95,
    "description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
    "category": "men's clothing",
    "image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg"
  },
  {
    "id": 2,
    "title": "Mens Casual Premium Slim Fit T-Shirts ",
    "price": 22.3,
    "description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
    "category": "men's clothing",
    "image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg"
  },
]

最后这是我出现的错误

I/flutter (31455): onChange -- AppCubit, Change { currentState: Instance of 'OnLoadingCategoriesState', nextState: Instance of 'OnCatchErrorCategoriesState' }
I/flutter (31455): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

任何帮助我将不胜感激

【问题讨论】:

    标签: flutter dart dio cubit


    【解决方案1】:

    CategoriesModel.fromJson 期待 Map&lt;String, dynamic&gt; 类型,但 json sn-p 显示响应是 List

    更新.then函数以处理列表并从List中的每个项目创建CategoriesModel,类似于上面sn-p中注释掉的代码。

     DioHelper.getData(pathUrl: "products").then((List values) {
         values.map((e) => CategoriesModel.fromJson(e)).toList();
     }
    

    【讨论】:

    • 非常感谢您的回复,但现在出现错误:未为“响应”类型定义“地图”方法。
    • 更新getData函数检查Response并从响应中返回数据。
    • 对不起,Rohan 我听不到你,但是当返回响应时它是 List 对象数组.. 感谢您的帮助
    • 你的意思是地图
    • 我在上面的答案中涉及的问题。
    猜你喜欢
    • 2020-11-12
    • 2021-12-23
    • 1970-01-01
    • 2021-06-09
    • 2021-04-13
    • 1970-01-01
    • 2021-07-14
    • 2021-03-25
    • 1970-01-01
    相关资源
    最近更新 更多