英文:
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<String, Object> toMap() {
    return {
      'step': step,
      'description': description,
    };
  }
  factory TutorialStep.fromJson(Map<String, Object> json) => TutorialStep(
        step: json['step'] as String,
        description: json['description'] as String,
      );
  static List<TutorialStep> toList(List<Map<String, Object>> json) {
    return List.from(json)
        .map((e) => TutorialStep(step: e['step'], description: e['description'])).toList();
  }
}
class Ingredient {
  String name;
  String size;
  Ingredient({required this.name, required this.size});
  factory Ingredient.fromJson(Map<String, Object> json) => Ingredient(
        name: json['name'] as String,
        size: json['size'] as String,
      );
  Map<String, Object> toMap() {
    return {
      'name': name,
      'size': size,
    };
  }
  static List<Ingredient> toList(List<Map<String, Object>> json) {
    return List.from(json)
        .map((e) => Ingredient(name: e['name'], size: e['size']))
        .toList();
  }
}
and this the recipe_helper.dart:
import 'package:recipe/model/recipe.dart';
class RecipeHelper {
  static List<Recipe> newlyPostedRecipe = newlyPostedRecipeRawData
      .map((data) => Recipe(
            title: data['title'] as String,
            photo: data['photo'] as String,
            description: data['description'] as String,
            tutorial: TutorialStep.toList(data['tutorial']),
            ingredients: Ingredient.toList(data['ingredients']),
          ))
      .toList();
}
the error is in the line tutorial: TutorialStep.toList(data['tutorial']), and ingredients: Ingredient.toList(data['ingredients']), 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['tutorial']),
        ingredients: Ingredient.toList(data['ingredients']),
to
        tutorial: TutorialStep.toList(data['tutorial'] as List<Map<String, Object>>),
        ingredients: Ingredient.toList(data['ingredients'] as List<Map<String, Object>>),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论