颤振钩在哪里“请求权限”,因为它没有 initState

问题描述

我正在学习 Flutter Hooks,但是当你需要一个通常在 initState 中的特定权限请求时,我找不到任何关于使用什么的信息,例如:

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    _requestPermissions();
    _configureDidReceiveLocalNotificationSubject();
    _configureSelectNotificationSubject();
  }

  void _requestPermissions() {
    flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.requestPermissions(
          alert: true,badge: true,sound: true,);
    flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            MacOSFlutterLocalNotificationsPlugin>()
        ?.requestPermissions(
          alert: true,);
  }...

现在迁移它以使用钩子 initState 和 dispose 不是“需要”的,因为它可以为您处理它,这在许多情况下都很好,但我无法将头放在何处放置此类请求权限,例如?

如何在使用 class HomePage extends HookWidget 时请求这些权限?

解决方法

解决此问题的最简单方法是使用 StatefulHookWidget 而不是 HookWidgetStatefulHookWidget 的行为与 StatefulWidget 一样,但您可以在构建方法中使用钩子。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...