Nik Evi
Nik Evi

Reputation: 62

FutureBuilder rebuilds because of TextField in Flutter

The problem is when I try to input some text into textfield, the keyboard shows up for a second, and then the whole FutureBuilder rebuilds immediately. Here is my FutureBuilder

body: FutureBuilder(
          future: Provider.of<Activities>(context, listen: false).fetchAndSet(),
          builder: (context, snapshot) => snapshot.connectionState ==
                  ConnectionState.waiting
              ? Center(child: CircularProgressIndicator())
              : Consumer<Activities>(
                  child: Center(
                    child: const Text('nothing here'),
                  ),
                  builder: (context, activities, ch) => SingleChildScrollView(
                    child: Center(
                      child: Column(children: [
                        Container(
                          padding:
                              EdgeInsets.only(top: mediaQuery.size.height / 40),
                          child: Text(time, style: TextStyle(fontSize: 30)),
                        ),
                        DayContainer(mediaQuery, activities),
                        Container(
                          height: 300,
                          child: SingleChildScrollView(
                            physics: ScrollPhysics(),
                            child: Column(
                              children: <Widget>[
                                ListView.builder(
                                    physics: NeverScrollableScrollPhysics(),
                                    shrinkWrap: true,
                                    itemCount: activities.activities.length,
                                    itemBuilder: (context, index) {
                                      return ElementPicker(
                                          mediaQuery,
                                          // this.callbackN,
                                          activities.activities[index]);
                                    })
                              ],
                            ),
                          ),
                        ),
                        FloatingActionButton(
                          onPressed: () => activities.addActivity(
                              '0Adding0', 'Добавить активность', 0.0, 0.0),
                          child: Icon(Icons.add),
                        )
                      ]),
                    ),
                  ),
                ),
        )

Inside of ElementPicker, there is another widget in which there is TextField

Upvotes: 2

Views: 1915

Answers (1)

Calvin Gonsalves
Calvin Gonsalves

Reputation: 2030

You need to initialize your futures in the initState so that whenever the widget rebuilds for any reason (in this case opening of the keyboard), you don't perform the fetchAndSet again:

void initState() {
 super.initState();
 fetchAndSetFuture = Provider.of<Activities>(context, listen: false).fetchAndSet();
}
...

//in build()

FutureBuilder(
 future: fetchAndSetFuture,
 builder: ...
);

Upvotes: 5

Related Questions