FutureBuilder before build method?

Viewed 37

Is it possibile to call the future of a FutureBuilder and retrieve the snapshot before the actual build of the builder? In order to avoid the waiting of connectionState.waiting.
In code:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                       child: ClipPath(
                        clipper: ShapeBorderClipper(
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                        ),
                        child: Theme(
                          data: Theme.of(context).copyWith(
                              dividerColor: Colors.transparent),
                          child: ExpansionTile(
                            
                            title: const Text(
                              "In corso",
                              
                            children: [
                              FutureBuilder(
                                  future: _future,
                                  builder: (context,
                                      AsyncSnapshot<
                                          List<DocumentSnapshot>>
                                      snapshot) {
                                    if (snapshot.hasData) {
                                      List<String> schedineincorso = [];

                                      for (var DOC in snapshot.data!) {
                                        schedineincorso.add(DOC.id);
                                      }
                                      return Column(
                                        children: [
                                          // children...
                                        ],
                                      );
                                    } else {
                                      return const CircularProgressIndicator();
                                    }
                                  })
                            ],
                          ),
                        ),
                    ),
                  ),
                  
        ],
      ),
    );
  }

I'd like the data to be ready before the user expands the Expansion Tile, because at the moment when a user clicks on the Expansion Tile it shows the CircularProgressIndicator for a while.

1 Answers

If you have some version of the data before the future completes (like an older version from before a server refresh, or from another page, as you mention) then you can pass that as the initialValue argument of the FutureBuilder constructor. Per the documentation, initialValue is the data that will be used to create the snapshots provided until a non-null future has completed.

Related