gpt4 book ai didi

flutter - 如何将appState(提供者)传递到initState的屏幕中?

转载 作者:行者123 更新时间:2023-12-03 04:24:28 25 4
gpt4 key购买 nike

您好我有这个问题

dependOnInheritedWidgetOfExactType<_DefaultInheritedProviderScope>() or dependOnInheritedElement() was called before _NavigationBarState.initState() completed.



我试图将 AppState的值传递到屏幕的内容都在 initState中。
initState:
@override
void initState() {
final appState = Provider.of<AppState>(context, listen: true);
screens = [
Home(),
Search(),
AddPost(),
Notifications(),
Profile(appState.currentUser),
];
super.initState();
}

还有我的 AppState:
class AppState extends ChangeNotifier {
AppState() {
_fetchAll();
}
// User
var currentUser = CurrentUser();
var lastUser = LastUser();

Future<void> _fetchCurrentUser() async {
currentUser.fetchUser('0');
}

Future<void> _fetchAll() async {
print('fetching');
_fetchCurrentUser();
notifyListeners();
}
}

应该如何实现?

最佳答案

您不能在listen: true中使用initState。使用listen: false代替:

@override
void initState() {
super.initState();
Provider.of<Whatever>(context, listen: false);
}

关于flutter - 如何将appState(提供者)传递到initState的屏幕中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60489288/

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