Home > front end >  FutureBuilder display all my data even if my query filter
FutureBuilder display all my data even if my query filter

Time:11-18

I have a function where I get all chapter of a specific book :

  Future<List> getChapters() async {
    allChapterList.clear();
    var collectionRef = await firestore
        .collection("chapters")
        .orderBy('number', descending: false)
        .where('manga', isEqualTo: _currentMangaTitle)
        .get();
    final allChapterData = collectionRef.docs.map((doc) => doc.data()).toList();
    allChapterList.addAll(allChapterData);

    return allChapterList;
  }

You can see the where statement. But when I launch the page, I have all the chapters, even if they are not in the current book. I need to refresh to just have the chapters wanted. all chapters after refresh

Here the FutureBuilder :

FutureBuilder(
                          future: _allData,
                          builder: (context, AsyncSnapshot future) {
                            if (!future.hasData) {
                              return Container();
                            } else {
                              var chapters = future.data;
                              return RefreshIndicator(
                                  onRefresh: () {
                                    return Future(() {
                                      getChapters();
                                      setState(() {});
                                    });
                                  },
                                  child: ListView.builder(
                                    shrinkWrap: true,
                                    itemCount: chapters.length,
                                    itemBuilder:
                                        (BuildContext context, int index) {
                                      String checkIfReaded() {
                                        if (readChaptersTitle.contains(
                                            chapters[index]['title'])) {
                                          isRead = 'Vu';
                                        } else {
                                          isRead = '';
                                        }
                                        return isRead;
                                      }

                                      checkIfReaded();

                                      return ListTile(
                                        onTap: () {
                                          Navigator.push(context,
                                              MaterialPageRoute(builder: (_) {
                                            return ChapterDetailsPage(
                                                chapterdata: chapters[index]);
                                          }));
                                        },
                                        title: Text(chapters[index]['title']),
                                        subtitle: Text(
                                            "n° ${chapters[index]['number']}"),
                                        leading: Container(
                                          clipBehavior: Clip.hardEdge,
                                          decoration: BoxDecoration(
                                              borderRadius:
                                                  BorderRadius.circular(10)),
                                          width: 6.h,
                                          height: 6.h,
                                          child: Image.network(
                                            chapters[index]['pic'],
                                            fit: BoxFit.cover,
                                          ),
                                        ),
                                        trailing: Row(
                                            mainAxisSize: MainAxisSize.min,
                                            children: [
                                              Text(isRead),
                                              IconButton(
                                                icon: Icon(
                                                  Icons.info_outlined,
                                                  color: Theme.of(context)
                                                      .primaryColorLight,
                                                ),
                                                onPressed: () {
                                                  Navigator.push(context,
                                                      MaterialPageRoute(
                                                          builder: (_) {
                                                    return ChapterDetailsPage(
                                                        chapterdata:
                                                            chapters[index]);
                                                  }));
                                                },
                                              ),
                                            ]),
                                      );
                                    },
                                  ));
                            }
                          }),

It will be a big problem if user need to refresh manually every time they want to see chapters of a book.

EDIT : Here _allData :

  late Future<List<dynamic>> _allData;

  void initState() {
    _allData = getChapters();
    _readData = getReadChapters();
    _currentMangaTitle = widget.mangadata['title'];
    super.initState();
  }

CodePudding user response:

You read the chapters before you have set the title.

  void initState() {
    _allData = getChapters(); // <-- here, __currentMangaTitle is nott yet set
    _readData = getReadChapters();
    _currentMangaTitle = widget.mangadata['title'];
    super.initState();
  }

You should probably move the line _currentMangaTitle = widget.mangadata['title']; to the top of that function.

  • Related