1

I am new to flutter and right now I'm trying to implement local notification on android on button click (With the local notification package). I tried multiple tutorials with different approaches but I end up always getting the same Error:

"[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field '_instance@...' has not been initialized."

here is my code of my service "notification_service"

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationApi {
  static final _notifications = FlutterLocalNotificationsPlugin();

  static Future _notificationDetails() async {
    return NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        'channel description',
        importance: Importance.max,
      ),
      iOS: IOSNotificationDetails(),
    );
  }

  static Future showNotification({
    int id = 0,
    String? title,
    String? body,
    String? payload,
  }) async =>
    _notifications.show(
      id,
      title,
      body,
      await _notificationDetails(),
      payload: payload
    );

}

And here my code where I try to implement my notification method, on button pressend:

import 'package:flutter/material.dart';
import 'package:my_goal_app/services/notification_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';


class NewsWidget extends StatefulWidget {

  @override
  State<NewsWidget> createState() => _NewsWidgetState();
}

class _NewsWidgetState extends State<NewsWidget> {

  @override
  Widget build(BuildContext context) {
   return Scaffold(
        appBar: AppBar(
                    title: Text('News')
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () => NotificationApi.showNotification(
              title: 'What a great title',
              body: 'Hey! This is a Body!',
              payload: 'sarah.abs',
            ),
            child: Text('click me'),
          ),
        ));
        }
  }

As I already said I tried out different tutorials on youtube but got the same problem. I tried to edit the AndroidManifest but nothing worked. And of course I tried solutions like this LateInitializationError: Field 'data' has not been initialized, got error or this

I am working on this problem for several days now and can't figure this out. I would be very pleased if anyone could help me. If I let out any information, please tell me, thats my first post.

Greirat
  • 11
  • 4

1 Answers1

0
  static Future _notificationDetails() async {
    return const NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        'channel description',
        icon: 'ic_launcher',
        importance: Importance.max,
      ),
      iOS: IOSNotificationDetails(),
    );
  }

Can you add ic_launcher in drawable folder and add icon in AndroidNotificationDetails ?

and Uninstall previous app and install again then run?

Balaji
  • 1,773
  • 1
  • 17
  • 30
  • I used your code and using the icon helped! Thanks. But I did several mistakes more, I was in debug mode and the error was just a breakpoint + I didnt allow notificaitons on the emulator. – Greirat Apr 15 '23 at 20:04