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.