作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好我有这个问题
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/
我是一名优秀的程序员,十分优秀!