【问题标题】:Unhandled Exception: A Follows was used after being disposed.Once you have called dispose() on a Follows, it can no longer be used未处理的异常:在处理后使用了一个关注。一旦你在关注上调用了 dispose(),它就不能再使用了
【发布时间】:2020-12-27 12:02:20
【问题描述】:

我是状态管理的新人,对提供程序包很感兴趣。 产生这些类型的异常有多少不同的原因,我该如何解决, 此异常是在 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 方法的位置 未处理的异常:在处理后使用了跟随。 E/flutter (8465):一旦你在一个 Follows 上调用了 dispose(),它就不能再使用了。

【问题讨论】:

    标签: flutter provider flutter-provider


    【解决方案1】:
    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 对象尝试更新前一个对象

    【讨论】:

      【解决方案2】:

      我也有同样的问题。

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

      为我工作。

      【讨论】:

        猜你喜欢
        • 2020-04-16
        • 2020-12-09
        • 2021-01-01
        • 2020-04-26
        • 2021-07-10
        • 2022-01-16
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多