Welcome to ZaiZheLe Developer Zone-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

flutter - unable to update state after async call

So am having a scroll listener where am calling the function to get data but after getting data i need to hot restart to see changes. and is it like that set state rebuilds the main widget but not any sub widget like i made a widget xyz which is of container type and placed this in main widget so will it rebuild xyz also?

listener:-

void _scrollListener()async{
    if (!loading&&!load) {
      if (scrollController.position.pixels == scrollController.position.maxScrollExtent) {
        setState(() => load = true);
        await clearEmptyDocs();
        getMoreData();
      }
    }
  }

function:-

getMoreData()async
  {
    var quicks;
    var posts;
    if(publicPostsDocuments.isNotEmpty)
      {
        print('1



');
        posts=await getPublicPosts();
      }
    else
      {
        print('2



');
        posts=await getPublicDocuments();
      }
    if(publicQuicksDocuments.isNotEmpty)
    {
      print('3



');
      quicks=await getPublicQuicks();
    }
    else
    {
      print('4



');
      quicks=await getPublicDocumentsForQuicks();
    }
    List<GridTile> newGridTile=[];
    posts.forEach((eachPost) {newGridTile.add(GridTile(
      child: ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: PostTile(eachPost,posts: postList,)
      ),
    ));});
    quicks.forEach((eachPost) {newGridTile.add(GridTile(
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: PostTile(eachPost,posts: quicksList,isFromSearch: true,),
      ),
    ));});
    newGridTile.shuffle();
    gridTile.addAll(newGridTile); //am using this gridtile list to show elements
    setState(() {
      load=false;
    });
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use FutureBuilder like this:

FutureBuilder(
      future: _scrollListener,
      builder: (context, snapshot) {
         return snapshot.data != null
           ? Text('there is no data'):
            Text('load the thing you want to display'),
       },
),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to ZaiZheLe Developer Zone-Open, Learning and Share
...