未处理的异常:在 disposed.Once 之后使用了 Follows 你已经在 Follows 上调用了 dispose(),它不能再被使用

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

我是 state Management 的新手,正在使用 provider package。 产生这些类型的异常有多少种不同的原因,我该如何解决, 当在 didChangeDependencies.

中调用 getFollowing() 方法 时会生成此异常

Follows.dart

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 =[];
      data.forEach((user){    
        followingData.add(Follow(
          id: user["_id"],
          username: user["username"],
          fullname: user["fullname"],
          imageUrl: user["imageUrl"],
          followerCount : (user["followers"] as List).length 
        ));

      });
      _following = [...followingData];
  notifyListeners();
      
}

 .........

}

Main.dart

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

FollowList.dart

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


FollowList({this.id});
  @override
  _FollowListState createState() => _FollowListState();
}

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

  @override
  void didChangeDependencies() {
    setState(() {
     isLoading = true; 
    });
      Provider.of<Follows>(context,listen: false).getFollowing(widget.id).then((_){
    setState(() {
      isLoading = false; 
      }); 
    });
    super.didChangeDependencies();
  }
  @override
  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,
        ),
    );
  }
}

请指定调用 dispose 方法的位置 未处理的异常:A Follows 在处理后被使用。 E/flutter ( 8465): 一旦你在关注上调用了 dispose() ,它就不能再使用了。

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)
 ),

如果 ChangeNotifier 更新为新值,listen: false 将保留旧对象的引用而不是创建新的 Follows 对象尝试更新前一个对象

和我一样的问题。

我带 "Future.delayed" 来应用下面已解决的问题,

Future.delayed

[/] 你的 MultiProvider 正确。

@override
void didChangeDependencies() {
  setState(() {
    isLoading = true;
  });
  Future.delayed(Duration(milliseconds: 300)).then((_) async {
    await Provider.of<Follows>(context, listen: false)
        .getFollowing(widget.id)
        .then((_) {
      setState(() {
        isLoading = false;
      });
    });
  });
  super.didChangeDependencies();
}

为我工作。