flutter_background_service
flutter_background_service copied to clipboard
get data from restful api
hi i want to get data from api inside onstart method but whenever i did it give me a error which is
Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>'
i dont know even data is parse but it still show me this error
void onStart()async {
WidgetsFlutterBinding.ensureInitialized();
final service = FlutterBackgroundService();
Timer.periodic(Duration(seconds: 1), (timer) async {
if (!(await service.isServiceRunning())) timer.cancel();
service.setNotificationInfo(
title: "abc",
content: "Lecture Updated at ${DateTime.now()}",
);
service.sendData(
{
"current_date": DateTime.now().toIso8601String(),
},
);
});
HttpRequest request = HttpRequest();
List list = await request.getList();// here give me error
print('list is ${list[0]['start']}');
}
You are trying to send data in same function, try to learn "How method and event channel works". There is a link for you: https://medium.com/@igaurab/event-channels-in-flutter-2b4d0db0ee4f You should receive data from main. Also try to send data, another functions. Have a good day!
You are trying to send data in same function, try to learn "How method and event channel works". There is a link for you: https://medium.com/@igaurab/event-channels-in-flutter-2b4d0db0ee4f You should receive data from main. Also try to send data, another functions. Have a good day!
this is a method which i call in main.dart where i get data from another function of another class suppose dashboard.dart
void onStart() async {
WidgetsFlutterBinding.ensureInitialized();
final service = FlutterBackgroundService();
Timer.periodic(Duration(seconds: 1), (timer) async {
if (!(await service.isServiceRunning())) timer.cancel();
service.sendData(
{
"current_date": DateTime.now().toIso8601String(),
},
);
});
// here i get data from dashboard.dart class where data is saved in sqflite
service.onDataReceived.listen(
(event) {
List value = jsonDecode(event!['class_time']['data']);
print('event value is ${value.length}');
for (int i = 0; i < value.length; i++) {
if (compareTime(value[i], value[i])) {
// showNotification();
}
}
},
);}
here is dashboard.dart
List valueList=[];
final db = await database; //sqflite database reference
List list = ['12:40', '12:45', '12:50', '12:55'];//suppose data came from api
valueList.clear();
for (int i = 0; i < list.length; i++) {
if (!valueList.contains(list)) {
valueList.add(list[i]);
}
}
Map<String, Object?> map = {
'data': jsonEncode(valueList),
};
await db.execute("DELETE FROM class_time_table");
await db.insert(
'class_time_table',
map,
conflictAlgorithm: ConflictAlgorithm.replace,
);
List<Map> maps = await db.query('class_time_table');
// here i sent data to services from getting sqflite data ,
FlutterBackgroundService().sendData({
'class_time': maps[0],
});
it work only when app is foreground but when app is background or kill it did not work i dont know why i tried all possible solution that i know but failed, i want that this data from dashboard.class saved in sqflite and then sent to background services and background service run show message at given time. i am new in flutter so i dont know about method channel or event channel
When and where does your dashboard.dart run? I wrote that "How method and event channel works" you should review for it. If you don't read these topics, unfortunately you won't be able to understand the working of this library. Also if you define dashboard.dart like class in onStart method actually you dont need to send data. The send and receive methods work to communicate background and foreground processes. Because each dart file is isolated, it runs as separate processes. If you want to research this topic: https://medium.com/dartlang/dart-asynchronous-programming-isolates-and-event-loops-bffc3e296a6a
dashboard.class is another class that hold a data which shown above and it came when app run , and main.dart came only when app restart .
Only onStart method will be working on background. If you are trying to receive data from front-end when your app is closed, it will give this error.
I missed something about my last comment bytheway. If we talk about ios background services, you should edit your codes with xcode, try to open xcode, find background fetches, allow notification and background processing. Thank you!