Unhandled Exception: A Follows was used after being disposed.Once you have called dispose() on a Follows, it can no longer be used


I am new in state Management in flutter with provider package .
How many different cause for generate these types of exception and How can I fix it,
this exception was generate when getFollowing() method was called in didChangeDependencies.


class Follows with ChangeNotifier{
  List<Follow> _following =[];
  String userid;
  String token;
  List<Follow> get followingUser{
    return [..._following];

  void updates(String token,String userid){
    this.userid = userid;
    this.token =  token;


 Future<void> getFollowing(String  id) async {

      final response = await http.get("${Domain.ADDRESS}/user/following/$id",headers: {"auth-token" : this.token});
      final data =json.decode(response.body)["following"] as List;
      List<Follow> followingData =[];
          id: user["_id"],
          username: user["username"],
          fullname: user["fullname"],
          imageUrl: user["imageUrl"],
          followerCount : (user["followers"] as List).length 

      _following = [...followingData];




 return MultiProvider(
        providers: [
            create: (ctx) => Auth(),
          ChangeNotifierProxyProvider<Auth , Follows>(
        create: (ctx)=>Follows(),
        update : (context, auth, previous) => Follows()..updates(auth.token, auth.userId)
    child : .......


class FollowList extends StatefulWidget {
static const followRoutes = "/follow-list";
final String id;

  _FollowListState createState() => _FollowListState();

class _FollowListState extends State<FollowList> {
  bool isLoading = false;

  void didChangeDependencies() {
    setState(() {
     isLoading = true; 
      Provider.of<Follows>(context,listen: false).getFollowing(widget.id).then((_){
    setState(() {
      isLoading = false; 
  Widget build(BuildContext context) {
     List<Follow> following = Provider.of<Follows>(context,listen: false).followingUser;
    return Scaffold(
      appBar: AppBar(title: Text("following),),
      body: isLoading ?  Center(child: CircularProgressIndicator(strokeWidth: 1,))
       : ListView.builder(
          itemBuilder: (context, index) => UserCard(
            id: following[index].id,
            fullname :following[index].fullname,
            username :following[index].username,
            followerCount : following[index].followerCount,
            imageUrl: following[index].imageUrl,
            followPressed: true,
          itemCount: following.length,

Please specify where dispose method was called for
Unhandled Exception: A Follows was used after being disposed.
E/flutter ( 8465): Once you have called dispose() on a Follows, it can no longer be used.


ChangeNotifierProxyProvider<Auth , Follows>(
        create: (ctx) => Follows(),
        //update : (context, auth, previous) => Follows()..updates(auth.token, auth.userId) 
        // You're creating a new Follow object and disposing the old one
        update: (context, auth, previous) => previous..updates(auth.token, auth.userId)

Instead of creating a new Follows object try to update the previous one, the listen: false will keep the reference of the old object if the ChangeNotifier updates to the new value

Answered By – EdwynZN

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *