I am using workmanager package for Home screen widget app for running background tasks

235 views Asked by At

I am getting error only in release mode but when i try the same in debug method it runs fine and fetched periodic data once for every 15 mins. The error i encountered after building apk is

E/flutter (29096): [ERROR:flutter/runtime/dart_isolate.cc(667)] Could not resolve main entrypoint function.
E/flutter (29096): [ERROR:flutter/runtime/dart_isolate.cc(168)] Could not run the run main Dart entrypoint.
E/flutter (29096): [ERROR:flutter/runtime/runtime_controller.cc(463)] Could not create root isolate.
E/flutter (29096): [ERROR:flutter/shell/common/shell.cc(645)] Could not launch engine with configuration.

These are my function and callbackdispatcher for workmanager

Future<String> fetchAlbum() async {
  final response = await http
      .get(Uri.parse(''));

  if (response.statusCode == 200) {
    final data = json.decode(response.body);
    final result = data as Map<String, dynamic>;
    allApiData = result;
    leadNumber = allApiData['totaladleads'].toString();
    print('Total lead number is $leadNumber');
  } else {
    throw Exception('Failed to load user data');
  }
  return leadNumber;
}


@pragma('vm:entry-point')
void callbackDispatcher() {
  Workmanager().executeTask((taskName, inputData) async {
    if(taskName == 'widgetBackgroundUpdate'){
      final leadCount = await fetchAlbum();
        HomeWidget.saveWidgetData(
          'title',
          leadCount,
        );
        HomeWidget.saveWidgetData(
          'message',
          'Total leads',
        );
        HomeWidget.updateWidget(
          name: 'HomeWidgetExampleProvider',
          iOSName: 'HomeWidgetExample',
        );
    }
    return Future.value(true);
  });
}

I am expecting to fetch the data from api for every 15 mins using Work manager plugin with Home widget package.

1

There are 1 answers

0
Devendiran On BEST ANSWER

Firstly we need to add @pragma("vm:entry-point") and then we need to check for the Key we have assigned in register periodic task and also need to assign it to the call back function. That's it.