0

我正在学习 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,
          badge: true,
          sound: true,
        );
  }...

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

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

4

1 回答 1

0

解决这个问题的最简单方法是使用 aStatefulHookWidget而不是 a HookWidgetStatefulHookWidget行为与 the 一样,StatefulWidget但您可以在 build 方法中使用钩子。

于 2021-07-06T13:41:34.103 回答