gpt4 book ai didi

flutter - 未处理的异常 : A Follows was used after being disposed. 在 Follows 上调用 dispose() 后,将无法再使用

转载 作者:行者123 更新时间:2023-12-02 19:15:10 30 4
gpt4 key购买 nike

我是 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 ): 一旦你在 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)
),

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

关于flutter - 未处理的异常 : A Follows was used after being disposed. 在 Follows 上调用 dispose() 后,将无法再使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63805124/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com