代码之家  ›  专栏  ›  技术社区  ›  acincognito

flatter map.addAll()导致失败

  •  0
  • acincognito  · 技术社区  · 6 年前

    List<Map> 颤振时:

    List<Map<String, dynamic>> recipeList = [
      {
    'name': 'rec1',
    'id': 1,
    'img': 'images/recipe.jpg',
    'ingredients': [{
      'name': 'salt',
      'amount': '1',
      'unit': '1',
    },
    {
      'name': 'flour',
      'amount': '100',
      'unit': 'g',
    },
    {
      'name': 'water',
      'amount': '100',
      'unit': 'g',
    },
    {
      'name': 'milk',
      'amount': '100',
      'unit': 'g',
    },],
    },]
    

    Widgets 在某个时刻,我想添加键值对 {'didBuy':false} 每一个 Map recipeList['ingredients'] ). 因此我称之为:

    List<Map<String, dynamic>> resultList = recipeList['ingredients'].map((elem) {
      elem.addAll({'didBuy': false});
      print(elem);
    }).toList();
    

    Dart Error: Unhandled exception:type '_InternalLinkedHashMap<String, bool>' is not a subtype of type 'Map<String, String>' of 'other' .

    有人知道在没有收到这个错误信息的情况下,向地图添加内容的正确方法是什么吗?

    把问题编辑得更准确些。

    编辑2: 在调用 List 明确地在 地图 正如Hadrien所建议的,我可以添加带有布尔值的键值对。长期以来,我想从互联网上获取数据,所以我定义了一个RecipeObj:

    class RecipeObj{
    
      String name;
      int id;
      String img;
      List<Map<String, dynamic>> ingredients;
    
      RecipeObj(this.name, this.id, this.img, this.ingredients);
    
    }
    

    List<Map<String, String>> ,尽管我把它定义为 List<Map<String, dynamic>> ,为什么?

    3 回复  |  直到 6 年前
        1
  •  4
  •   Hadrien Lejard    6 年前

    快速推断你的配料表的类型 Map<String, String>

    您可以在列表中自己指定类型

    'ingredients': <Map<String, dynamic>>[ {
      'name': 'salt',
      'amount': '1',
      'unit': '1',
     },
    

    或者建造一个新的 Map<String, dynamic> 在你的 map 功能

     List<Map<String, dynamic>> resultList = recipeList['ingredients'].map((elem) {
      final map = Map<String, dynamic>.from(elem);
      map.addAll({'didBuy': false});
      return map;
     }).toList();
    
        2
  •  1
  •   Günter Zöchbauer    6 年前

    这样就可以了

    List<Map<String,dynamic>> recipeList = [
    

    至少如果 recipeList ingredients 指向同一集合实例。

    var ingredients = recipeList;
    
        3
  •  0
  •   Feu    6 年前

    这是你需要的吗?

    void main() {
    
    List<Map> recipeList = [
      {
    'name': 'rec1',
    'id': 1,
    'img': 'images/recipe.jpg',
    'ingredients': [{
      'name': 'salt',
      'amount': '1',
      'unit': '1',
    },
    {
      'name': 'flour',
      'amount': '100',
      'unit': 'g',
    },
    {
      'name': 'water',
      'amount': '100',
      'unit': 'g',
    },
    {
      'name': 'milk',
      'amount': '100',
      'unit': 'g',
    },]
    },];
    
      print("[DATA BEFORE ANY CHANGE]");
      print("recipeList.length=${recipeList.length}");
      print("recipeList[0][\"ingredients\"]=${recipeList[0]["ingredients"]}");
      print("recipeList[0][\"ingredients\"].last=${recipeList[0]["ingredients"].last}"); 
      print("recipeList[0][\"ingredients\"].length=${recipeList[0]["ingredients"].length}");
    
      // no recipe is worth if it doesn't contain chocolate
      recipeList[0]["ingredients"].add({
        'name': 'cocoa powder',
        'amount': '200',
        'unit': 'g',    
      });
    
      print("\n\n[DATA AFTER ADD]");
      print("recipeList[0][\"ingredients\"].last=${recipeList[0]["ingredients"].last}");
      print("recipeList[0][\"ingredients\"].length=${recipeList[0]["ingredients"].length}");
    }
    

    [DATA BEFORE ANY CHANGE]
    recipeList.length=1
    recipeList[0]["ingredients"]=[{name: salt, amount: 1, unit: 1}, {name: flour, amount: 100, unit: g}, {name: water, amount: 100, unit: g}, {name: milk, amount: 100, unit: g}]
    recipeList[0]["ingredients"].last={name: milk, amount: 100, unit: g}
    recipeList[0]["ingredients"].length=4
    
    
    [DATA AFTER ADD]
    recipeList[0]["ingredients"].last={name: cocoa powder, amount: 200, unit: g}
    recipeList[0]["ingredients"].length=5