1

I want to launch notifications periodically if a condition is checked

class notification definition

FlutterLocalNotificationsPlugin notificationsPlugin =
    FlutterLocalNotificationsPlugin();

class NotificationLocal extends StatefulWidget {
  static const routeName = '/notificaton';
  const NotificationLocal({Key? key}) : super(key: key);

  Future<void> displayNotification() async {
    notificationsPlugin.zonedSchedule(
        0,
        'Alerte Sécurité :',
        'Attention l\'aire est toxique pensez à aéré la pièce',
        tz.TZDateTime.now(tz.local).add(Duration(seconds: 5)),
        NotificationDetails(
          android: AndroidNotificationDetails(
              'channel id', 'channel name', 'channel description',
              importance: Importance.high, priority: Priority.high),
        ),
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
        androidAllowWhileIdle: true);
  }

  @override
  _NotificationLocalState createState() => _NotificationLocalState();
}

class _NotificationLocalState extends State<NotificationLocal> {
  @override
  void initState() {
    // TODO: implement initState

    initializeSetting();
    tz.initializeTimeZones();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: ElevatedButton(
      style: ElevatedButton.styleFrom(
          primary: Colors.green,
          padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
          textStyle: TextStyle(fontSize: 30, fontWeight: FontWeight.bold)),
      // textColor: Colors.white,
      // color: Colors.blue,
      child: Text('Login'),
      onPressed: () {
        (new NotificationLocal()).displayNotification();
      },
    ));
  }
}

void initializeSetting() async {
  var initializeAndroid = AndroidInitializationSettings('warn');
  var initializeIOS = IOSInitializationSettings();
  var initializeSetting =
      InitializationSettings(android: initializeAndroid, iOS: initializeIOS);
  await notificationsPlugin.initialize(initializeSetting);
}

when i Try to call the code below in a another class

  @override
  void initState() {
    super.initState();
    Timer.periodic(Duration(milliseconds: 10000), (timer) {
      (new NotificationLocal()).displayNotification();
    });

I got the following errors

E/flutter (20095): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: LateInitializationError: Field '_local@732310200' has not been initialized.
E/flutter (20095): #0 _local (package:timezone/src/env.dart)
E/flutter (20095): #1 local (package:timezone/src/env.dart:31:23)
E/flutter (20095): #2 NotificationLocal.displayNotification (package:weather_app/service/notification_service.dart:18:30)
E/flutter (20095): #3 _MyAppState.initState. (package:weather_app/api/api_post.dart:53:33)
E/flutter (20095): #4 _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (20095): #5 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (20095): #6 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (20095): #7 _CustomZone.bindUnaryCallbackGuarded. (dart:async/zone.dart:1207:26)
E/flutter (20095): #8 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter (20095): #9 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (20095): #10 _CustomZone.bindUnaryCallback. (dart:async/zone.dart:1191:26)
E/flutter (20095): #11 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter (20095): #12 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter (20095): #13 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter (20095):

1 Answer 1

0

First install timezone plugin with following command:

flutter pub add timezone

After that add the following code:

import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

void main() async {
tz.initializeTimeZones();
runApp(const MyApp());
}

Best regards.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.