Deepak Lohmod
Deepak Lohmod

Reputation: 2272

Want to run a third function on completion on of 2 functions

Actually, I am getting a list of documents from function one and similarly another list from the second function. After getting them i want to merge the list and display in list view.

Function 1:-

getPublicQuicks()async
  {
    int count=0;
    List<Post> newQuicks=[];
    for(var element in publicQuicksDocuments){
      count++;
      if(element.postIdList.isNotEmpty)
      {
        DocumentSnapshot doc=await quickReference.document(element.id).collection('userQuicks').document(element.postIdList.last).get();
        element.postIdList.removeLast();
        newQuicks.add(Post.fromDocument(doc));
      }
      else
      {
        emptyPublicDocForQuicks.add(element);
      }
      if(count>publicQuicksDocuments.length-1)
      {
        newQuicks.shuffle();
        quicksList.addAll(newQuicks);//This is the first list
        setState(() {
          loading=false;
          load=false;
        });
      }
    }
  }

Function 2:-

Second()async
  {
    int count=0;
    List<Post> newPosts=[];
    for(var element in publicPostsDocuments){
      count++;
      if(element.postIdList.isNotEmpty)
        {
          DocumentSnapshot doc=await postsReference.document(element.id).collection('userPosts').document(element.postIdList.last).get();
          element.postIdList.removeLast();
          newPosts.add(Post.fromDocument(doc));
        }
      else
        {
          emptyPublicDoc.add(element);
        }
      if(count>publicPostsDocuments.length-1)
      {
        newPosts.shuffle();
        postList.addAll(newPosts);//This is 2nd list
        setState(() {
          loading=false;
          load=false;
        });
      }
    }
  }

Now i want to call function when execution of both of these function is over. Am calling these functions in my initState. So, how shall i call the 3rd function?

Upvotes: 0

Views: 27

Answers (1)

fvillalba
fvillalba

Reputation: 1058

You can call a async method on initState and then update the state when you have the two results, something like:

 @override
  void initState() {
    super.initState();
    asyncMethod();
  }

  void asyncMethod() async {
    final res1= await asyncCall1();
    final res2= await asyncCall2();
    final _list = mergeList(res1, res2);
    setState(() {
        list=_list;
    });
    // ....
  }

Upvotes: 1

Related Questions