dart 如何使你的代码运行时,应用程序是终止在Flutter?

kgsdhlau  于 2023-06-19  发布在  Flutter
关注(0)|答案(1)|浏览(106)

我希望我的flutter应用程序能够从我的rest API和flutter本地通知插件接收通知,而不需要firebase,workmanager或前台服务。如果有人有办法,请帮帮我。

void get Notifications()async{
  var Notifications = await Db.get("notifications","/*Notification id*/");
FlutterLocalNotificationsPlugin.show();
}
gcxthw6b

gcxthw6b1#

您可以使用Cron Package来完成此操作。
1.将cron包添加到pubspec.yaml文件中:

dependencies:
  cron: ^x.x.x  # Replace with the latest version

2.在Dart文件中导入必要的包:

import 'package:flutter/material.dart';
import 'package:cron/cron.dart';

3.定义将在指定时间执行的函数:

void myTask() {
  // Your task to be executed
  // This code will be executed according to the cron expression
  // Perform your desired operations here
}

4.使用cron包安排任务:

void scheduleCronTask() {
  final cron = Cron();

  // Schedule the task using a cron expression
  // The example below will run the task every day at 12 am
  cron.schedule(Schedule.parse('0 0 * * *'), myTask);
}

void main() {
  runApp(MyApp());

  // Schedule the cron task
  scheduleCronTask();
}

相关问题