Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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>>),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading