【发布时间】:2020-10-13 17:08:21
【问题描述】:
我有一个问题,我在一个文件(Recipe.dart)中有一个名为 fetchAll() 的方法,我需要它来返回我的食谱列表,为此我创建了一个 Future:
Future<List<Recipe>> recipes() async {
// Get a reference to the database.
final Database db = await database;
// Query the table for all The Recipes.
final List<Map<String, dynamic>> title = await db.query('Rezepte');
final List<Map<String, dynamic>> ingredients = await db.query('Zutaten');
final List<Map<String, dynamic>> preperation =
await db.query('Zubereitungen');
final List<Map<String, dynamic>> imgUrl = await db.query('Images');
// Convert the List<Map<String, dynamic> into a List<Recipe>.
return List.generate(title.length, (i) {
return Recipe(
id: i,
name: title[i]['Rezept_Title'],
ingredients: ingredients[i]['Zutat'],
preperation: preperation[i]['Zubereitung'],
imageURL: imgUrl[i]['Image_URl'],
);
});
}
但这是在数据库帮助文件中,否则我无法访问数据库,那么如何连接这些?那么函数 fetchAll() 返回的是未来收集的 Recipes 列表?
【问题讨论】: