【发布时间】:2020-12-11 11:07:38
【问题描述】:
我试图在退出页面时将 INT 值保存在共享首选项中。所以我使用 dispose 方法保存,但它给出了错误。
试图从小部件树外部侦听提供者公开的值。
This is likely caused by an event handler (like a button's onPressed) that called
Provider.of without passing `listen: false`.
To fix, write:
Provider.of<AuthProvider>(context, listen: false);
It is unsupported because may pointlessly rebuild the widget associated to the
event handler, when the widget tree doesn't care about the value.
The context used was: PlayPage(dependencies: [_InheritedProviderScope<AuthProvider>, MediaQuery],
state: _PlayPageState#d7691(lifecycle state: defunct))
'package:provider/src/provider.dart':
Failed assertion: line 242 pos 7: 'context.owner.debugBuilding ||
listen == false ||
debugIsInInheritedProviderUpdate'
代码:
@override
void dispose() {
Provider.of<AuthProvider>(context).storeVideoTiming( _duration?.inSeconds ?? 0);
super.dispose();
}
提供者页面:
storeVideoTiming(int timins) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('VideoCount', timins);
}
【问题讨论】: