【问题标题】:Parsing JSON data into a List (Dart)将 JSON 数据解析为列表 (Dart)
【发布时间】:2021-06-12 11:44:50
【问题描述】:

出于某种原因,我为我的颤振应用程序从 Firbase 迁移到 Back4app 实时数据库。我有一个购物应用程序,并将我的产品数据存储到该数据库中。以前在 firebase 中,我有这样的代码来根据我的 JSON 数据创建一个新产品,这里我使用 foreach() 方法将产品添加到我的列表中,因为我的响应 JSON 正文是

    final List<Product> loadedProduct = [];
    var extractedData = jsonDecode(response.body) as Map<String, dynamic>;
  extractedData.forEach((k,v) { 

loadedProduct.add(Product(
     id: k,
     title: v['Title'],
     description:v['Description'], 
     price: v['Price'], 
     imageUrl: v['ImageUrl']));

     });

但是当我设置我的 Bakc4app 它有不同类型的 JSON 主体:

{results: [{objectId: K9RTu0LBgI, pTitle: laptop, pDescription: macbookpro 2018, pPrice: 60.58, pImageUrl: https://images.unsplash.com/photo-1541807084-5c52b6b3adef?ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8bGFwdG9wfGVufDB8fDB8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60, isFavorite: false, createdAt: 2021-06-11T10:42:39.185Z, updatedAt: 2021-06-11T10:42:39.185Z}, {objectId: CzzhXFKahu, pTitle: headphone, pDescription: best headphone for gaming , pPrice: 60.55, pImageUrl: https://images.unsplash.com/photo-1505740420928-5e560c06d30e?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aGVhZHBob25lfGVufDB8fDB8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60, isFavorite: false, createdAt: 2021-06-11T10:46:32.541Z, updatedAt: 2021-06-11T10:46:32.541Z}, {objectId: e5PiYuLDQZ, pTitle: apple watch, pDescription: apple watch seris 3, pPrice: 85.55, pImageUrl: https://images.unsplash.com/photo-1546868871-7041f2a55e12?ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8YXBwbGUlMjB3YXRjaHxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=

它包含一个带有地图列表的地图我如何基于这种类型的 JSON 创建新产品??

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:
    List<Product> loadedProduct;
        var responseBody= Map<String,dynamic>.from(jsonDecode(response.body));
        var resultList = List<Map<String, dynamic>>.from(responsBody["results"]);
        loadedProducts = resultList.map((Map<String, dynamic> productMap){
          return Product(
         id: productMap["objectId"],
         title: productMap["pTitle"],
         description:productMap['pDescription'], 
         price: double.parse(productMap['pPrice']), 
         imageUrl: productMap['pImageUrl']));
    
        }).toList()
    

    【讨论】:

    • [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:“double”类型不是“String”类型的子类型 Products.updateProductListServer.包: my_shop/providers/product_provider.dart:106 MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31) ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
    • 我可以看看您的Product 课程吗?您可以将价格定义为String
    • code class Product with ChangeNotifier { final String id;最终字符串标题;最终字符串描述;最终双倍价格;最终字符串 imageUrl;布尔是最喜欢的;产品({ @required this.id, @required this.title, @required this.description, @required this.price, @required this.imageUrl, this.isFavorite = false, }); code
    • 我的价格是双倍的
    • 我已经更新了答案试试看能否解决问题
    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2017-11-26
    • 2022-01-27
    • 2021-05-17
    • 2020-11-25
    相关资源
    最近更新 更多