颤抖如何延迟重新加载数据库?

时间:2020-07-02 16:31:25

标签: sql sqlite flutter dart lazy-loading

简而言之:

会发生什么:

  • 它一开始会获取10个食谱,每次您触底时都会重新加载相同的10个食谱
  • 当我想使用100种食谱时,它在开始时加载的时间就很长,而在总的来说,整个东西(列表视图生成器,延迟加载...)都毫无意义

我想要的:

  • 当我有一个包含100个配方的数据库时,我希望一开始就可以加载10个配方,然后每次我在最底层时都希望加载下10个(不同)配方。

长版:

您好,目前我有一个项目,需要从数据库中获取一些配方。到目前为止,一切都很好,但是当您位于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;
  }

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

0 个答案:

没有答案