Unable to retrieve Friends Flutter

I am calling retrieveFriends in initstate; however, eventhough I have friend relation with below code, I still do not get any data in _followerController.loadedItems. Why is this?

Future sendFollowRequest(String userId) async {
try {
final followStatus = await AmityCoreClient.newUserRepository()
.relationship()
.follow(userId);

  AppLogger.logInfo('sendFollowRequest: $followStatus');

  return followStatus;
} catch (exception) {
  AppLogger.logError("sendFollowRequest Failed: $exception");
  rethrow;
}

}

Future _retrieveFriends() async {
_followerController = PagingController(
pageFuture: (token) => AmityCoreClient.newUserRepository()
.relationship()
.getMyFollowers()
.getPagingData(token: token, limit: 20),
pageSize: 20,
)…addListener(
() {
setState(() {
if (_followerController.error == null) {
//handle _followerController, we suggest to clear the previous items
//and add with the latest _controller.loadedItems
_followRelationships.clear();
_followRelationships.addAll(_followerController.loadedItems);
//update widgets
} else {
//error on pagination controller
//update widgets
}
});
},
);

_followerController.fetchNextPage();

}

void _paginationListener() {
if (scrollController.position.pixels ==
scrollController.position.maxScrollExtent) {
_followerController.fetchNextPage();
}
}

Hello, may I confirm your current Flutter SDK version? Are there any errors occurring, or is it just empty?

We are currently using Flutter (Channel stable, 3.16.8, on macOS 14.3.1 23D60 darwin-arm64, locale en-KR) and we are able to retrieve and listen other functions like listening to globalfeedposts but for friends we get empty (listen method is not triggered because we believe friends is empty)

Hello @danielkang98 apologies if my question was unclear. We’re referring to Amity Flutter SDK version: Flutter (Beta) - Amity Docs, could you help confirm with version you’re on?

Hi @danielkang98, based on the code snippets you’ve shared, it appears you’re trying to retrieve a follower list for the current user. My suspicion is that you may not have accepted those follower requests yet. I recommend trying to retrieve the list with a ‘pending’ follower status in your query condition to see the current user’s pending follower requests. Please use the additional filter from the code snippet below:

AmityCoreClient.newUserRepository()
    .relationship()
    .getMyFollowers()
    .status(AmityFollowStatusFilter.PENDING)
    .getPagingData(token: token, limit: 20), pageSize: 20,
    ...

After the pending follower user list appears, make sure to accept the follow request using:

AmityCoreClient.newUserRepository()
    .relationship()
    .acceptMyFollower(userId = userId)
    .then((value) => {
        // success
    })
    ...

For more information, please refer to the documentation:

2 Likes

@Trust Hello I have tried status(AmityFollowStatusFilter.PENDING) and all the other amityfollowstatusfilter but I still am not fetching any followers. When I send follow request I get log info stating sendFollowRequest: AmityFollowStatus.ACCEPTED. What steps should I take? Thank you.

FYI I am using amity_sdk: ^0.34.0

Below reterieveFriends give me a log information “:bulb: Fetched 0 followers<…>”


  Future<void> _retrieveFriends() async {
    _followerController = PagingController(
      pageFuture: (token) => AmityCoreClient.newUserRepository()
          .relationship()
          .getMyFollowers()
          .status(AmityFollowStatusFilter.ALL)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          setState(() {
            if (_followerController.error == null) {
              //handle _followerController, we suggest to clear the previous items
              //and add with the latest _controller.loadedItems
              _followRelationships.clear();
              _followRelationships.addAll(_followerController.loadedItems);
              AppLogger.logInfo(
                  "Fetched ${_followRelationships.length} followers");
              //update widgets
            } else {
              //error on pagination controller
              //update widgets
            }
          });
        },
      );
  }
Future<AmityFollowStatus> sendFollowRequest(String userId) async {
    try {
      final followStatus = await AmityCoreClient.newUserRepository()
          .relationship()
          .follow(userId);

      AppLogger.logInfo('sendFollowRequest: $followStatus');

      return followStatus;
    } catch (exception) {
      AppLogger.logError("sendFollowRequest Failed: $exception");
      rethrow;
    }
  }

@danielkang98
From the issue you’re facing, here is an example code on how to retrieve follow data:

import 'dart:developer';

import 'package:amity_sdk/amity_sdk.dart';
import 'package:amity_uikit_beta_service/components/alert_dialog.dart';
import 'package:flutter/material.dart';

class FollowerVM extends ChangeNotifier {
  var _followerList = <AmityFollowRelationship>[];
  List<AmityFollowRelationship> get getFollowerList => _followerList;

  var _followingList = <AmityFollowRelationship>[];
  List<AmityFollowRelationship> get getFollowingList => _followingList;

  ScrollController? followingScrollController;

  ScrollController? followerScrollController;

  late PagingController<AmityFollowRelationship> _followerController;

  late PagingController<AmityFollowRelationship> _followingController;

  Future<void> getFollowingListof({required String userId}) async {
    log("getFollowingListOf....");
    if (AmityCoreClient.getUserId() == userId) {
      _followingController = PagingController(
        pageFuture: (token) => AmityCoreClient.newUserRepository()
            .relationship()
            .getMyFollowers()
            .status(AmityFollowStatusFilter.ACCEPTED)
            .getPagingData(token: token, limit: 20),
        pageSize: 20,
      )..addListener(listener);
    } else {
      _followingController = PagingController(
        pageFuture: (token) => AmityCoreClient.newUserRepository()
            .relationship()
            .getFollowings(userId)
            .status(AmityFollowStatusFilter.ACCEPTED)
            .getPagingData(token: token, limit: 20),
        pageSize: 20,
      )..addListener(listener);
    }
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _followingController.fetchNextPage();
    });

    if (followingScrollController != null) {
      _followingController.addListener((() {
        if ((followingScrollController!.position.pixels ==
                followingScrollController!.position.maxScrollExtent) &&
            _followingController.hasMoreItems) {
          _followingController.fetchNextPage();
        }
      }));
    }

    //inititate the PagingController
    if (AmityCoreClient.getUserId() == userId) {
      await AmityCoreClient.newUserRepository()
          .relationship()
          .me()
          .getFollowings()
          .status(AmityFollowStatusFilter.ACCEPTED)
          .getPagingData()
          .then((value) {
        log("getFollowerListOf....Successs");
        _followingList = value.data;
      }).onError((error, stackTrace) {
        AmityDialog()
            .showAlertErrorDialog(title: "Error!", message: error.toString());
      });
    } else {
      await AmityCoreClient.newUserRepository()
          .relationship()
          .user(userId)
          .getFollowings()
          .status(AmityFollowStatusFilter.ACCEPTED)
          .getPagingData()
          .then((value) {
        log("getFollowerListOf....Successs");
        followingScrollController = ScrollController();
        _followingList = value.data;
      }).onError((error, stackTrace) {
        AmityDialog()
            .showAlertErrorDialog(title: "Error!", message: error.toString());
      });
    }
    notifyListeners();
  }

  Future<void> getFollowerListOf({
    required String userId,
  }) async {
    log("getFollowerListOf....");
    if (AmityCoreClient.getUserId() == userId) {
      _followerController = PagingController(
        pageFuture: (token) => AmityCoreClient.newUserRepository()
            .relationship()
            .me()
            .getFollowers()
            .status(AmityFollowStatusFilter.ACCEPTED)
            .getPagingData(token: token, limit: 20),
        pageSize: 20,
      )..addListener(listener);
    } else {
      _followerController = PagingController(
        pageFuture: (token) => AmityCoreClient.newUserRepository()
            .relationship()
            .user(userId)
            .getFollowers()
            .status(AmityFollowStatusFilter.ACCEPTED)
            .getPagingData(token: token, limit: 20),
        pageSize: 20,
      )..addListener(listener);
    }
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _followerController.fetchNextPage();
    });

    if (followerScrollController != null) {
      _followerController.addListener((() {
        if ((followerScrollController!.position.pixels ==
                followerScrollController!.position.maxScrollExtent) &&
            _followerController.hasMoreItems) {
          _followerController.fetchNextPage();
        }
      }));
    }

    //inititate the PagingController
    if (AmityCoreClient.getUserId() == userId) {
      await AmityCoreClient.newUserRepository()
          .relationship()
          .me()
          .getFollowers()
          .status(AmityFollowStatusFilter.ACCEPTED)
          .getPagingData()
          .then((value) {
        log("getFollowerListOf....Successs");
        _followerList = value.data;
      }).onError((error, stackTrace) {
        AmityDialog()
            .showAlertErrorDialog(title: "Error!", message: error.toString());
      });
    } else {
      await AmityCoreClient.newUserRepository()
          .relationship()
          .user(userId)
          .getFollowers()
          .status(AmityFollowStatusFilter.ACCEPTED)
          .getPagingData()
          .then((value) {
        log("getFollowerListOf....Successs");
        followerScrollController = ScrollController();
        _followerList = value.data;
      }).onError((error, stackTrace) {
        AmityDialog()
            .showAlertErrorDialog(title: "Error!", message: error.toString());
      });
    }
    notifyListeners();
  }

  void followButtonAction(AmityUser user, AmityFollowStatus amityFollowStatus) {
    if (amityFollowStatus == AmityFollowStatus.NONE) {
      sendFollowRequest(user: user);
    } else if (amityFollowStatus == AmityFollowStatus.PENDING) {
      withdrawFollowRequest(user);
    } else if (amityFollowStatus == AmityFollowStatus.ACCEPTED) {
      withdrawFollowRequest(user);
    } else {
      AmityDialog().showAlertErrorDialog(
          title: "Error!",
          message: "followButtonAction: cant handle amityFollowStatus");
    }
  }

  Future<void> sendFollowRequest({required AmityUser user}) async {
    AmityCoreClient.newUserRepository()
        .relationship()
        .user(user.userId!)
        .follow()
        .then((AmityFollowStatus followStatus) {
      //success
      log("Follow Success");
      notifyListeners();
    }).onError((error, stackTrace) {
      //handle error
      AmityDialog()
          .showAlertErrorDialog(title: "Error!", message: error.toString());
    });
  }

  void withdrawFollowRequest(AmityUser user) {
    AmityCoreClient.newUserRepository()
        .relationship()
        .me()
        .unfollow(user.userId!)
        .then((value) {
      log("with Draw Success");
      notifyListeners();
    }).onError((error, stackTrace) {
      AmityDialog()
          .showAlertErrorDialog(title: "Error!", message: error.toString());
    });
  }

  Future<void> getPendingRequest() async {}

  Future<void> acceptFollowRequest(
      {required AmityFollowRelationship amityFollowRelationship}) async {}

  Future<void> rejectFollowRequest(
      {required AmityFollowRelationship amityFollowRelationship}) async {}

  Function listener() {
    return () {
      if (_followerController.error == null) {
        //handle _followerController, we suggest to clear the previous items
        //and add with the latest _controller.loadedItems
        _followerList.clear();

        _followerList.addAll(_followerController.loadedItems);
        //update widgets
      } else {
        //error on pagination controller
        //update widgets
      }
    };
  }
}
1 Like

Thank you! This solved the issue!

1 Like