periodically
如果检查了条件,我想启动通知
类通知定义
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);
}
当我尝试在另一个类中调用下面的代码时
@override
void initState() {
super.initState();
Timer.periodic(Duration(milliseconds: 10000), (timer) {
(new NotificationLocal()).displayNotification();
});
我收到以下错误
E/flutter (20095): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:LateInitializationError: 字段 '_local@732310200' 尚未初始化。
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) :