1

I have this StreamSubscription field called followSubscribtion. It listens if there is a new follower and then calls populateFollower to load follower profile.

   followsSubscription =
          getBloc(context).followsHandler.stream.listen((value) async {
        if (value.status == Status.success) {
        await populateFollows();
        }
      });
    });
  populateFollows() async{
      if (getBloc(context).followsModel.length > 0) {
        for (var i = 0; i < getBloc(context).followsModel.length; i++)  {
          getBloc(context).loadFollowsProfile(getBloc(context).followsModel[i].userId);
          break;
        }
    }
  }

This works fine, But I want each profile that will be loaded to be added to a list, How do I do that?

loadFollowsProfile method

  loadFollowsProfile(int id , List<UserProfileModel> profileList) {
    getFollowsProfileHandler.addNetworkTransformerStream(
        Network.getInstance().getUserProfile(id), (_) {
      userProfileModelBloc = UserProfileModel.fromJson(_);

      profileList.add(userProfileModelBloc);

      return userProfileModelBloc;
    });
  }
Alex Maina
  • 443
  • 4
  • 10

1 Answers1

0

You can do this by setting up loadFollowsProfile() to return a UserProfileModel, adding that to a list in the for loop of populateFollows(), and then returning that list from populateFollows().

List<ProfileObject> populateFollows() async{
      List<ProfileObject> profileList = [];
      if (getBloc(context).followsModel.length > 0) {
        for (var i = 0; i < getBloc(context).followsModel.length; i++){
          profileList.add(getBloc(context).loadFollowsProfile(
              getBloc(context).followsModel[i].userId
          ));
          break;
        }
    }
    return profileList;
  }

followsSubscription =
          getBloc(context).followsHandler.stream.listen((value) async {
        if (value.status == Status.success) {
          profileList = await populateFollows();
        }
      });
    });
TarHalda
  • 1,050
  • 1
  • 9
  • 27
  • You have added `profileList` as a parameter in `loadFollowsProfile()` method, Does that mean I need to use it on that method. – Alex Maina Sep 22 '22 at 16:35
  • Yes, you need to add the profile you are loading to the `profileList` inside the `loadFollowsProfile()` method. (I am assuming you can't return values from `loadFollowsProfile()` because it's part of a `bloc`; if you can return values from that method, then you could just return the profile from `loadFollowsProfile()` and then add it to the list inside `populateFollows()`.) @AlexMaina – TarHalda Sep 22 '22 at 16:46
  • No working still – Alex Maina Sep 22 '22 at 16:46
  • Can you return the profile from the `loadFollowsProfile()` instead of passing in the list to modify? I'm not sure how your `getBloc()` method works. @AlexMaina – TarHalda Sep 22 '22 at 16:47
  • Yes, It is only returning one profile, I want a list of profiles, I have updated my question with loadFollowsProfile() method – Alex Maina Sep 22 '22 at 16:50
  • @AlexMaina I updated the code to reflect the `loadFollowsProfile()` code. If you can return a value from `loadFollowsProfile()`, you don't need to pass the list in. – TarHalda Sep 22 '22 at 16:56
  • I/flutter (30622): I KNOW THIS WILL WORK [null, null, null] – Alex Maina Sep 22 '22 at 17:09
  • @AlexMaina are you sure that the `userProfileModelBloc` isn't null when it's being returned? – TarHalda Sep 22 '22 at 17:15
  • Its returning only one profileObject – Alex Maina Sep 22 '22 at 17:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248300/discussion-between-bellrampion-and-alex-maina). – TarHalda Sep 23 '22 at 13:29