Arthur Becker
Arthur Becker

Reputation: 13

returning a FutureBuilder as the result of a FutureBuilder Flutter

I'm creating an add friend page in Flutter/Firebase. I used a StreamBuilder to see the changes of the username input. Then I used a FutureBuilder as the result of the StreamBuilder to get my List of User. To verify that the users aren't already in thisUser's friend list, I'm using an other FutureBuilder as the result of the first one. And I still do the same for the friend requests.

It works but I'm pretty sure that not the right way. Could someone explain me how to do that more properly?

StreamBuilder(
  stream: controller.usernameStream.stream,
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      String usernameSearched = snapshot.data!;
      return FutureBuilder(
        future: _userRepo.getUsersStartsByUsername(usernameSearched),
        builder: (context, snapshotUsers){
          if(snapshotUsers.connectionState == ConnectionState.done){
            if (snapshotUsers.hasData){
              return controller.futureBuilderFriends(thisFireUser, snapshotUsers);
            } else {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: Center(child: Text(AppLocalizations.of(context)!.noUsers)),
              );
            }
          }
          return Container();
        },
      );
    }
    return Container();
  },
)
FutureBuilder<List<String>> futureBuilderFriends(User? thisFireUser, AsyncSnapshot<List<U.User>> snapshotUsers) {
    return FutureBuilder(
      future: _userRepo.getFriendsId(thisFireUser!.uid),
      builder: (BuildContext context, AsyncSnapshot<List<String>> snapshotFriends) {
        if (snapshotFriends.connectionState == ConnectionState.done) {
          if(snapshotFriends.hasData){
            return futureBuilderMyFriendRequests(thisFireUser, snapshotFriends.data!, snapshotUsers);
          } else {
            return futureBuilderMyFriendRequests(thisFireUser, [], snapshotUsers);
          }
        } else {
          return Container();
        }
      },
    );
  }
FutureBuilder<List<String>> futureBuilderMyFriendRequests(User thisFireUser, List<String> friends, AsyncSnapshot<List<U.User>> snapshotUsers) {
    return FutureBuilder(
      future: _userRepo.getFriendRequestsId(thisFireUser.uid),
      builder: (BuildContext context, AsyncSnapshot<List<String>> snapshotFriendRequests) {
        if (snapshotFriendRequests.connectionState == ConnectionState.done) {
          if(snapshotFriendRequests.hasData){
            return buildAddFriends(context, snapshotUsers.data!, thisFireUser, friends, snapshotFriendRequests.data!);
          } else {
            return buildAddFriends(context, snapshotUsers.data!, thisFireUser, friends, []);
          }
        } else {
          return Container();
        }
      },
    );
  }

Upvotes: 0

Views: 44

Answers (1)

Manjun Vasudevan
Manjun Vasudevan

Reputation: 286

Looks like you are on vanilla architecture. Please make use of bloc or Riverpod and use the transfrom functions and emit the states accordingly

Upvotes: 1

Related Questions