【问题标题】:Save in shared preference while exit page flutter退出页面颤动时保存在共享首选项中
【发布时间】: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);
    }

【问题讨论】:

    标签: flutter sharedpreferences


    【解决方案1】:

    只需更改这一行:

    Provider.of<AuthProvider>(context).storeVideoTiming( _duration?.inSeconds ?? 0);
    

    与:

    Provider.of<AuthProvider>(context, listen: false).storeVideoTiming( _duration?.inSeconds ?? 0);
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2021-08-16
      • 2013-07-27
      • 1970-01-01
      相关资源
      最近更新 更多