I'm using a mock json file called recipe.json. My intentions are to load the data when user enters the page. But whenever I hot reload the page nothing loads. If I want to load the data I have to save the file which then loads all the json data.
Here's the function that fetches the data:
List<RecipeModel> recipes = [];
Future<List<RecipeModel>> getRecipeData() async {
// var response = await http.get(
// Uri.https("jsonplaceholder.typicode.com", 'users'),
// );
String response = await DefaultAssetBundle.of(context)
.loadString('assets/json/recipe.json');
var result = json.decode(response);
for (var u in result["data"]) {
RecipeModel recipe = RecipeModel(
id: u['id'] ?? "",
name: u['name'] ?? "",
videoLink: u['videoLink'] ?? "",
author: u['author'] ?? "",
category: u['category'] ?? "",
time: u['time'] ?? "",
);
recipes.add(recipe);
}
print(recipes.length);
return recipes;
}
@override
void initState() {
super.initState();
getRecipeData();
}
Here's the ListView:
Container(
height: 250.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: recipes.length,
itemBuilder: (BuildContext context, int index) {
return RecipeCard(
itemName: recipes[index].name,
categoryName: recipes[index].category,
time: recipes[index].time);
},
),
),
My RecipeCard is a stateless widget which has itemname, categoryName and time marked as required.
Here's a fraction of the code from RecipeCard.dart:
class RecipeCard extends StatelessWidget {
final String itemName;
final String categoryName;
final String time;
RecipeCard(
{required this.itemName, required this.categoryName, required this.time});
@override
Widget build(BuildContext context) {}
}