未处理的异常:Follows 在被释放后被使用。一旦你对 Follows 调用了 dispose(),它就不能再被使用

2024-03-06

我是使用提供程序包进行颤振状态管理的新手。 有多少不同的原因会产生这些类型的异常,我该如何修复它, 这个异常是在以下时间生成的getFollowing() 方法被叫进来didChangeDependency.

跟随.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();
      
}

 .........

}

主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):一旦对 Follows 调用了 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)
 ),

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未处理的异常:Follows 在被释放后被使用。一旦你对 Follows 调用了 dispose(),它就不能再被使用 的相关文章

随机推荐