在 null 上调用了方法“initialiazeNotification”。接收器:null 尝试调用:initialiazeNotification()

The method 'initialiazeNotification' was called on null. Receiver: null Tried calling: initialiazeNotification()

提问人:Artyom 提问时间:8/6/2023 最后编辑:Artyom 更新时间:8/6/2023 访问量:22

问:

我对空的东西有问题,我的错误是“在 null 上调用了方法'initialiazeNotification'。 接收器:null 尝试调用:initialiazeNotification()”

我尝试逐步删除NotifyHelper类中的代码以查找错误,但没有任何效果

这是我的主要功能

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GetStorage.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: Themes.light,
      darkTheme: Themes.dark,
      themeMode: ThemeService().theme,
      home: const HomePage(),
    );}}

这是我的主页块,我在其中初始化了所有可验证的内容、类和财务

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  var notifyHelper;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    notifyHelper.initialiazeNotification();
    notifyHelper=NotifyHelper();
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
        AndroidFlutterLocalNotificationsPlugin>()?.requestPermission();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _appBar(),
      body: const Column(
        children: [
          Text("Theme Data",
          style: TextStyle(
            fontSize: 30
          ),)
        ],
      ),
    );
  }
  _appBar(){
    return AppBar(
      leading: GestureDetector(
        onTap: (){
          ThemeService().switchTheme();
          notifyHelper.displayNotification(
            title:'Тема изменилась' ,
            body: Get.isDarkMode?'black':'light'
          );
        },
        child: const Icon(Icons.nightlight_round,
        size: 20,),
      ),
      actions: const [
        Icon(Icons.person,
          size: 20,),
        SizedBox(width: 20,)
      ],);}}

这是我尝试为我的应用程序执行本地通知的通知 blick

class NotifyHelper {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  initializeNotification() async {
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('appicon');
    final DarwinInitializationSettings initializationSettingsDarwin =
        DarwinInitializationSettings(

            requestAlertPermission: false,
            requestBadgePermission: false,
            requestSoundPermission: false,
            onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    final InitializationSettings initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid,
        iOS: initializationSettingsDarwin);
    await flutterLocalNotificationsPlugin.initialize(
        initializationSettings,
        onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);
  }

  displayNotification({required String? title, required String? body}) async {
    print("doing test");
    var androidPlatformChannelSpecifics = const AndroidNotificationDetails(
        'your channel id', 'your channel name',
        channelDescription: 'Хуйня',
        importance: Importance.max, priority: Priority.high);
    var iOSPlatformChannelSpecifics = const DarwinNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
      0,
      'You change your theme',
      'You changed your theme back !',
      platformChannelSpecifics,
      payload: 'It could be anything you pass',
    );
  }

  void onDidReceiveNotificationResponse(NotificationResponse? notificationResponse) async {
    final String? payload = notificationResponse?.payload;
    if (payload != null) {
      print ("notification payload: $payload");
    } else {print('Notification done'); }
    Get.to(() => Container(color: Colors.white,));
  }



  Future onDidReceiveLocalNotification(
      int id, String? title, String? body, String? payload) async {
    Get.dialog(Text('welcome to flutter'));
  }
}

我尝试一步一步地删除代码,添加了?在可验证的内容和功能中,但没有任何效果

Flutter null 通知

评论

0赞 Artyom 8/6/2023
我刚刚编辑了我的代码,你能检查一下吗?
0赞 Artyom 8/7/2023
我找到了一个解决方案,我们需要在主模块中初始化异步函数,我不知道为什么,但它有效,任何人都可以解释我,为什么我只能通过 main 来做?

答: 暂无答案