“The argument type ‘Object?’ can’t be assigned to the parameter type ‘List>’.”

huangapple go评论62阅读模式
英文:

Getting this error: "The argument type 'Object?' can't be assigned to the parameter type 'List<Map<String, Object>>'."

问题

The error you're encountering in the recipe_helper.dart file is likely due to the data type mismatch. To resolve it, you can try updating the lines as follows:

tutorial: TutorialStep.toList(data['tutorial'] as List<Map<String, Object>>),
ingredients: Ingredient.toList(data['ingredients'] as List<Map<String, Object>>),

By explicitly casting data['tutorial'] and data['ingredients'] to List<Map<String, Object>>, you should resolve the error.

英文:

i want to try to make models. And here's the code

...

class TutorialStep {
  String step;
  String description;
  TutorialStep({required this.step, required this.description});

  Map&lt;String, Object&gt; toMap() {
    return {
      &#39;step&#39;: step,
      &#39;description&#39;: description,
    };
  }

  factory TutorialStep.fromJson(Map&lt;String, Object&gt; json) =&gt; TutorialStep(
        step: json[&#39;step&#39;] as String,
        description: json[&#39;description&#39;] as String,
      );

  static List&lt;TutorialStep&gt; toList(List&lt;Map&lt;String, Object&gt;&gt; json) {
    return List.from(json)
        .map((e) =&gt; TutorialStep(step: e[&#39;step&#39;], description: e[&#39;description&#39;])).toList();
  }
}

class Ingredient {
  String name;
  String size;

  Ingredient({required this.name, required this.size});
  factory Ingredient.fromJson(Map&lt;String, Object&gt; json) =&gt; Ingredient(
        name: json[&#39;name&#39;] as String,
        size: json[&#39;size&#39;] as String,
      );

  Map&lt;String, Object&gt; toMap() {
    return {
      &#39;name&#39;: name,
      &#39;size&#39;: size,
    };
  }

  static List&lt;Ingredient&gt; toList(List&lt;Map&lt;String, Object&gt;&gt; json) {
    return List.from(json)
        .map((e) =&gt; Ingredient(name: e[&#39;name&#39;], size: e[&#39;size&#39;]))
        .toList();
  }
}

and this the recipe_helper.dart:

import &#39;package:recipe/model/recipe.dart&#39;;

class RecipeHelper {
  static List&lt;Recipe&gt; newlyPostedRecipe = newlyPostedRecipeRawData
      .map((data) =&gt; Recipe(
            title: data[&#39;title&#39;] as String,
            photo: data[&#39;photo&#39;] as String,
            description: data[&#39;description&#39;] as String,
            tutorial: TutorialStep.toList(data[&#39;tutorial&#39;]),
            ingredients: Ingredient.toList(data[&#39;ingredients&#39;]),
          ))
      .toList();
}

the error is in the line tutorial: TutorialStep.toList(data[&#39;tutorial&#39;]), and ingredients: Ingredient.toList(data[&#39;ingredients&#39;]), at the recipe_helper.dart

I have tried adding as List at the end of the error, but I still get an error.

So, how to solve the error? Any help would be appreciated. Thanks

答案1

得分: 1

尝试更改

tutorial: TutorialStep.toList(data['tutorial']),
ingredients: Ingredient.toList(data['ingredients']),

tutorial: TutorialStep.toList(data['tutorial'] as List<Map<String, Object>>),
ingredients: Ingredient.toList(data['ingredients'] as List<Map<String, Object>>),

英文:

Try change

        tutorial: TutorialStep.toList(data[&#39;tutorial&#39;]),
        ingredients: Ingredient.toList(data[&#39;ingredients&#39;]),

to

        tutorial: TutorialStep.toList(data[&#39;tutorial&#39;] as List&lt;Map&lt;String, Object&gt;&gt;),
        ingredients: Ingredient.toList(data[&#39;ingredients&#39;] as List&lt;Map&lt;String, Object&gt;&gt;),

huangapple
  • 本文由 发表于 2023年6月8日 21:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定