簡體   English   中英

Flutter / Firebase:頁面間刷新FutureBuilder數據

[英]Flutter / Firebase : Refresh FutureBuilder data between pages

我是一名大三學生,我有一個應用程序,用戶可以在其中閱讀書籍。 我想在應用程序推薦其他書籍的地方添加一個功能。 我使用 Firestore 來存儲我的數據。 所以在這里我從 Firestore 獲取數據:

Future<List> getMoreChapter() async {
    final currentMangaChapters = await FirebaseFirestore.instance
        .collection('chapters')
        .where('manga', isEqualTo: _currentChapterManga)
        .get();
    moreChapter.clear();
    final currentMangaChaptersList =
        currentMangaChapters.docs.map((doc) => doc.data()).toList();
    genreReco.addAll(currentMangaChaptersList);

    return genreReco;
  }

然后我將結果顯示到 ListView.builder 和 Future.builder 中:

FutureBuilder(
                  future: getMoreChapter(),
                  builder: (context, AsyncSnapshot future) {
                    if (!future.hasData) {
                      return Container();
                    } else {
                      var moreChaptersList = future.data;
                      return SizedBox(
                        height: 140,
                        child: ListView.builder(
                          scrollDirection: Axis.horizontal,
                          itemCount: moreChaptersList.length,
                          itemBuilder: (context, index) {
                            return Padding(
                                padding: const EdgeInsets.all(5),
                                child: SizedBox(
                                    width: 90,
                                    height: 90,
                                    child: SingleChildScrollView(
                                        child: Column(children: [
                                      Image.network(
                                        moreChaptersList[index]['pic'],
                                        height: 90,
                                        width: 90,
                                      ),
                                      const SizedBox(
                                        height: 5,
                                      ),
                                      Text(
                                        moreChaptersList[index]['title'],
                                        style: const TextStyle(
                                            fontSize: 10,
                                            fontWeight: FontWeight.bold),
                                        textAlign: TextAlign.center,
                                      ),
                                      const SizedBox(height: 4),
                                      Text(
                                          '${moreChaptersList[index]['number']}',
                                          style: const TextStyle(fontSize: 10)),
                                    ]))));
                          },
                        ),
                      );
                    }
                  })

它工作正常,但問題是當用戶換到另一本書時,應用程序仍然顯示以前的推薦。

例如,第一本書: 第一本書示例

這是第二個: 第二本書的例子

您可以在底部看到問題。 我認為使用 Future.builder 不是最佳選擇。 你有什么建議嗎?

好的,所以對於那些想知道的人來說,問題是因為我沒有返回好的列表,而是來自其他功能的另一個......

小心點 !

Future<List> getMoreChapter() async {
    final currentMangaChapters = await FirebaseFirestore.instance
        .collection('chapters')
        .where('manga', isEqualTo: _currentChapterManga)
        .get();
    moreChapter.clear();
    final currentMangaChaptersList =
        currentMangaChapters.docs.map((doc) => doc.data()).toList();
    genreReco.addAll(currentMangaChaptersList);

    return currentMangaChaptersList;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM