0

I have an asynchronous function that obtains information from my bd in firebase, when debugging this code fragment I can see that the data is obtained without any problem, this data will be used to display in widgets and I pass it through a future builder, the problem is that although when debugging I realize that the data are there, Future builder does not detect them and snapshot has null value, it is until after several iterations when snapshot finally has data and allows me to use them, I do not understand what is wrong in the construction of my Future Builder.

Here is the code of my function where I get the data and the construction of the Future Buider.

Function where data are obtained.

 Future<List<Guide>> getGuidesList() async {
    var guidesProvider = Provider.of<GuidesProvider>(context, listen: false);
    Checkuser data = await ManagerDB().checkuser(auth.email);
    List<Guide> aux = new List();
    Guide guide;
    List guides = await guidesProvider.setGuidesFromUser(data);
    if (guides != null) {
      for (var i = 0; i < guides.length; i++) {
        await guides[i].get().then((DocumentSnapshot guides) {
          guide = Guide.fromMap(guides.data(), guides.reference.path);
          aux.add(guide);
        });
      }
      if (this.mounted) {
        setState(() {});
      }
      print('Guias cargadas correctamente');
      return aux;
    } else {
      print('Lista vacia');
      return aux;
    }
  }

Fragmento de Funcion donde creo mi FutureBuider.

return Scaffold(
      resizeToAvoidBottomInset: false,
      key: _scaffoldKey,
      appBar: appBar,
      drawer: DrawerNavigationMenu(
        getContext: widget.getcontext,
      ),
      body: FutureBuilder<List<Guide>>(
        future: getGuidesList(),
        builder: (BuildContext context, snapshot) {
          if (snapshot.hasData) {
            return ListCourses(
              getContext: widget.getcontext,
              items: snapshot.data,
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );

1 Answers1

0
if (this.mounted) {
        setState(() {});
      }

Delete this part. You are unnecessarily rebuilding your scaffold and re-calling FutureBuilder. Let FutureBuilder take care of processing the future and rebuilding the scaffold for you.

Scott
  • 371
  • 4
  • 8