简而言之:
会发生什么:
我想要的:
长版:
您好,目前我有一个项目,需要从数据库中获取一些配方。到目前为止,一切都很好,但是当您位于Listview的底部时,我该如何总是再增加10个食谱? 我已经有一个获取食谱的功能,当您接近底部时,我有一个可以加载更多食谱的功能。但是这里出现了我的问题,这些功能在一起重新加载总是相同的10 (我知道为什么但不知道如何解决)食谱,所以我需要一些知道多少配方的东西已经被提取等等。这是我的代码:
_loadmore()函数:
void _loadMore() {
_isLoading = true;
_itemFetcher.fetchRecipes().then((List<Recipe> fetchedList) {
if (fetchedList.isEmpty) {
setState(() {
_isLoading = false;
_hasMore = false;
});
} else {
setState(() {
_isLoading = false;
_recipeList.addAll(fetchedList);
});
}
});
}
还有fetchRecipes()函数:
Future<List<Recipe>> fetchRecipes({int fetchFromIndex1, int fetchToIndex1}) async {
final db = await dbProvider.database;
int fromIndex = fetchFromIndex1 ?? 0;
int toIndex = fetchToIndex1 ?? 10;
final List<Map<String, dynamic>> title = await db.rawQuery('SELECT * FROM Rezepte WHERE Rezept_ID >=$fromIndex AND Rezept_ID <= $toIndex');
final List<Map<String, dynamic>> preperation =
await db.rawQuery('SELECT * FROM Zubereitungen WHERE Zubereitungs_ID >=$fromIndex AND Zubereitungs_ID <= $toIndex');
final List<Map<String, dynamic>> imgUrl = await db.rawQuery('SELECT * FROM Images WHERE Rezept_ID >=$fromIndex AND Rezept_ID <= $toIndex');
final List<Map<String, dynamic>> ingredients =
await db.rawQuery('SELECT * FROM Zutaten WHERE Rezept_ID >= $fromIndex AND Rezept_ID <= $toIndex');
int id = 0;
List<Map> parsedTitle = [];
List<Map> parsedPreperation = [];
List<Ingredient> parsedIngredient = [];
List<Map> jsonParsedIngredient = [];
// Convert the List<Map<String, dynamic> into a List<Recipe>.
Ingredient toIngredient(String title, String amount, String unit) {
return Ingredient(title, amount, unit);
}
ingredients.forEach(
(ing) => jsonParsedIngredient.add(Map<String, dynamic>.from(ing)));
jsonParsedIngredient
.forEach((r) => r['Zutat'] = new String.fromCharCodes(r['Zutat']));
jsonParsedIngredient
.forEach((r) => r['Einheit'] = new String.fromCharCodes(r['Einheit']));
jsonParsedIngredient
.forEach((r) => r['Menge'] = new String.fromCharCodes(r['Menge']));
ingredients.forEach(
(_ingredient) => parsedIngredient.add(
toIngredient(
String.fromCharCodes(_ingredient['Zutat']),
String.fromCharCodes(_ingredient['Menge']),
String.fromCharCodes(_ingredient['Einheit']),
),
),
);
// ignore: missing_return
List<Ingredient> getIngredient(int _id) {
String mying = jsonEncode(jsonParsedIngredient);
bool recipeReady = false;
List<Ingredient> toEncIngredients = [];
try {
while (!recipeReady) {
if (_id == jsonDecode(mying)[id]['Rezept_ID']) {
while (_id == jsonDecode(mying)[id]['Rezept_ID']) {
toEncIngredients.add(
toIngredient(
jsonDecode(mying)[id]['Zutat'],
jsonDecode(mying)[id]['Menge'],
jsonDecode(mying)[id]['Einheit']),
);
id += 1;
}
recipeReady = true;
} else {
id += 1;
}
}
return toEncIngredients;
} on Error {}
}
title.forEach((title) => parsedTitle.add(Map<String, dynamic>.from(title)));
preperation.forEach(
(title) => parsedPreperation.add(Map<String, dynamic>.from(title)));
// make Rezept_Title string again
parsedTitle.forEach(
(r) => r['Rezept_Title'] = new String.fromCharCodes(r['Rezept_Title']));
parsedPreperation.forEach(
(r) => r['Zubereitung'] = new String.fromCharCodes(r['Zubereitung']));
final List<Recipe> fullRecipe = List.generate(
title.length,
(i) {
return Recipe(
id: i,
name: parsedTitle[i]['Rezept_Title'],
ingredients: getIngredient(i),
preperation: parsedPreperation[i]['Zubereitung'].toString(),
imageURL: imgUrl[i]['Image_URL'],
);
},
);
inspect(fullRecipe);
return fullRecipe;
}
但是我认为从该功能中,有趣的部分是查询的第一行,其余的只是将其转换为列表。 希望您能帮助我。谢谢!