flutter如何用数据库惰性地重新加载?

gijlo24d  于 2021-07-29  发布在  Java
关注(0)|答案(0)|浏览(215)

简而言之:
发生了什么:
它在开始的时候取10个菜谱,每次你到达底部,它就会重新加载同样的10个菜谱
问题当我想使用100个食谱时,它在开始时加载的时间非常长,一般来说,所有的东西(listview生成器,延迟加载…)都没有意义
我想要的是:
当我有一个数据库,有100个食谱,我想在一开始也许10食谱加载,然后每次我在底部,我希望接下来的10个(不同的)食谱得到加载
较长版本:
你好,目前我有一个项目,我需要从数据库中获取一些食谱。到目前为止还不错,但是当你在列表视图的底部时,我如何获取更多的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;
  }

但我认为,从这个函数中,有趣的部分只是查询的第一行,其余部分只是将其转换为一个列表。我希望你能帮助我。谢谢您!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题