How to get data from the FutureProvider in flutter

后端 未结 2 2125
伪装坚强ぢ
伪装坚强ぢ 2021-02-15 13:21

I\'m trying to implement local database support in my flutter app which is being managed using Provider, now I want to make the retrieving of data obey the state management patt

2条回答
  •  温柔的废话
    2021-02-15 14:10

    In my example I used the create parameter of FutureProvider to request the API, then then I used Consumer to get the results of the API.

     FutureProvider(
            create: (_) => peopleService.getAllSurvivor(),
            child: Consumer>(builder: (context, survivors, _) {
              return survivors == null
                  ? Center(child: CircularProgressIndicator())
                  : ListView.builder(
                      itemCount: survivors.length,
                      itemBuilder: (context, index) {
                        var survivor = survivors[index];
                        return ListTile(
                          title: Text(survivor.name),
                          subtitle: Text(survivor.gender),
                          leading: Icon(Icons.perm_identity),
                        );
                      },
                    );
            })));
    

提交回复
热议问题