未处理的异常:处置后使用了追随者,一旦在追随者上调用了dispose后就无法再使用

问题描述

我是状态管理人员的新手,提供程序包扑朔迷离。 有多少种不同的原因会导致产生此类异常,我该如何解决, 在 didChangeDependencies 调用 getFollowing()方法时会生成此异常。

关注.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方法 未处理的异常:处置后使用了追随者。 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,previous) => previous..updates(auth.token,auth.userId)
 ),

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

,

我有同样的问题。

我带来"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();
}

为我工作。