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
>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>>),