dart /Flutter:自定义操作错误没有为类型定义getter 'shared'

z9ju0rcb  于 2023-09-28  发布在  Flutter
关注(0)|答案(2)|浏览(197)

我为这个错误伤透了脑筋。我谦卑地寻求一些帮助。
我一直收到下面的错误。然而,在GitHub repo上列出的示例中(这里:https://github.com/OneSignal/OneSignal-Flutter-SDK/blob/main/example/lib/main.dart),则显示.shared getter属性。我正在使用OneSignal Fluuter包(onesignal_flutter:^5.0.1)https://pub.dev/packages/onesignal_flutter

The getter 'shared' isn't defined for the type 'OneSignal'. Try importing the library that defines 'shared', correcting the name to the name of an existing getter, or defining a getter or field named 'shared'.

我做错了什么?
下面是我试图实现的操作:
InitOneSignal

import 'package:onesignal_flutter/onesignal_flutter.dart';

Future<void> initOneSignal() async {
  // Set the log level for debugging (optional)
  OneSignal.shared.setLogLevel(OSLogLevel.verbose);

  // Initialize OneSignal with your App ID
  await OneSignal.shared.init(
    "API_KEY", // Replace with your OneSignal App ID
    iOSSettings: {
      OSiOSSettings.autoPrompt:
          false, // Set to true if you want the default iOS prompt
      OSiOSSettings.inAppLaunchUrl: true,
    },
  );

  // Request permission from the user for notifications
  final status = await OneSignal.shared.getPermissionSubscriptionState();
  if (status.permissionStatus.status != OSNotificationPermission.authorized) {
    // Handle the case where notification permission is not authorized.
    // You can show a dialog or perform any other desired action here.
    // For example, you can request permission again or inform the user about the need for notifications.
  }
}

提前感谢!

up9lanfz

up9lanfz1#

错误消息“The getter 'shared' isn't defined for the type 'OneSignal'”表示OneSignal Flutter包没有名为shared的getter。这是因为共享getter在包的5.0.0版本中被删除了。
要修复此错误,您需要更改代码以使用新的OneSignal单例类而不是共享getter。举例来说:

import 'package:onesignal_flutter/onesignal_flutter.dart';
 Future<void> initOneSignal() async {
 // Set the log level for debugging (optional)
 OneSignal().setLogLevel(OSLogLevel.verbose);

 // Initialize OneSignal with your App ID
 await OneSignal().init(
 "API_KEY", // Replace with your OneSignal App ID
 iOSSettings: {
  OSiOSSettings.autoPrompt:
      false, // Set to true if you want the default iOS prompt
  OSiOSSettings.inAppLaunchUrl: true,
 },
);

 // Request permission from the user for notifications
 final status = await OneSignal().getPermissionSubscriptionState();
 if (status.permissionStatus.status != 
  OSNotificationPermission.authorized) {
  // Handle the case where notification permission is not authorized.
  // You can show a dialog or perform any other desired action here.
  // For example, you can request permission again or inform the user 
 about the need for notifications.
}
   }

OneSignal()单例类与共享getter相同,只是它不需要导入onesignal_flutter包。如果您正在使用依赖于OneSignal Flutter软件包的多个软件包,这可能很有用。

5cnsuln7

5cnsuln72#

这是由于5.0.0版本(请参见)。GitHub上的示例似乎是在该版本之前创建的。
请查看pub.dev上的OneSignal example,它使用5.0.0以上的版本。
你可以看到setLogLevel方法应该是:OneSignal.Debug.setLogLevel(OSLogLevel.verbose)init方法应为OneSignal.initialize
编辑:我在GitHub上打开了一个issue,以解决不匹配问题

相关问题