【问题标题】:How to have access Context in didChangeAppLifecycleState lifecycle hook using flutter HookWidget?如何使用flutter HookWidget访问didChangeAppLifecycleState生命周期钩子中的上下文?
【发布时间】:2020-12-14 03:32:03
【问题描述】:

我正在尝试访问上下文,以便我可以读取我的提供程序,但由于此生命周期挂钩位于小部件树之外。它无法访问。有没有办法访问上下文?

【问题讨论】:

  • 请提供您已经尝试过的代码清单。
  • 您需要提供更广泛的上下文。

标签: flutter flutter-layout flutter-dependencies


【解决方案1】:

我研究了一下,最后和flutter bloc社区的narcodico讨论了,所以学分是他的。

因此,在状态类上混入WidgetsBindingObservercontext 甚至在 didChangeAppLifecycleState 这样的覆盖中也可用,因为它们是状态类的一部分。

另外,请考虑移动到状态小部件上方的 BlocProvider。

例子

class HomePageProvider extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => getIt<InAppPurchasesBloc>(),
      child: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
...
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      context
          .read<InAppPurchasesBloc>()
          .add(const InAppPurchasesEvent.getPurchaserInfo());
    }
  }
...
}

【讨论】:

    【解决方案2】:

    恐怕您无法访问 didChangeAppLifecycleState 中的上下文。

    【讨论】:

      【解决方案3】:

      对于任何感兴趣的人,您可以将脚手架状态保存在全局键中,并从其当前状态访问上下文。

      【讨论】:

      • 你有例子吗?谢谢!
      • 我找到了一个解决方案并在下面发布了答案。
      • final GlobalKey&lt;ScaffoldState&gt; _scaffoldKey = new GlobalKey&lt;ScaffoldState&gt;(); 然后你可以像 `_scaffoldKey.currentState.context.read....` 一样访问它,确保你将你的密钥添加到 Scaffold 小部件
      • 您可以接受我的解决方案,因为它是正确的。
      【解决方案4】:

      你可以使用useEffect函数,阅读更多: https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useEffect.html;

      Widget build(BuildContext context) {
      useEffect(() {
      
          //what would you write in initState
      
        },
      );
      

      【讨论】:

      • 使用效果在build context里面,我的function在外面
      【解决方案5】:

      您可以考虑使用Riverpod 包而不是Provider。 Riverpod 与 Provider 出自同一作者,被认为是“更好的 Provider”,但有许多改进,包括 Flutter 独立性,这意味着它不依赖上下文来工作,您可以使用几乎与 provider 相同的方式。

      使用 Riverpod 和 Flutter Hooks,您可以执行以下操作:

      // create a provider in a global context
      final myProvider = Provider((ref) => myClass());
      
      // access the provider inside your class
      class MyWidget extends HookWidget{
      
        //access the provider using a hook
        final myClassProvider = useProvider(myProvider);
        //... your logic
        @override
        Widget build (BuildContext context){/* ... build widget tree... */}
      }
      

      考虑this very useful and concise tutorial 如何将 Riverpod 与 Flutter Hooks 和 StateNotifier、ChangeNotifier 等一起使用...

      【讨论】:

      • 我正在使用riverpod。当我尝试在 didChangeAppLifecycleState 函数中使用提供程序时,我得到“useContext”只能从 HookWidget 的构建方法中调用
      猜你喜欢
      • 1970-01-01
      • 2023-01-13
      • 2020-01-15
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多