Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter workmanager plugin doesn't work with any other plugin when running task

After initialising the the workmanager and creating either of the tasks, If we use any plugins inside of the task execution it isn't recognised and throw an error as bellow MissingPluginException(No implementation found for method getLocation on channel lyokone/location)

Actual code :

Workmanager.executeTask((task, inputData) async {
  Location locationObject = Location(); 
  locationObject.getLocation(); 
  print(locationObject); 
  return Future.value(true); 
}

Basically any other plugin used inside of the task of work manager seems to be not recognised.

What am I missing, Do I need to register all my plugins again ?

I/flutter (16120): Location permission has error

I/flutter (16120): MissingPluginException(No implementation found for method serviceEnabled on channel lyokone/location)

like image 232
Vivek C R Avatar asked Oct 02 '19 02:10

Vivek C R


1 Answers

If want to use other plugins from inside the WorkManager executeTask then; custom application class needs to be created and the other plugins needs to be registered. If WorkManager plugin itself needs to be used inside execute task that plugin also needs to be registered. Also this newly created custom application needs to be specified in AndroidManifest.xml file. This is mentioned in the plugin's issues link as it can't be handled completely from the plugin itself.

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.androidalarmmanager.AlarmService;
import io.flutter.plugins.androidalarmmanager.AndroidAlarmManagerPlugin;
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin;
import io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin;
import be.tramckrijte.workmanager.WorkmanagerPlugin;

public class CustomApplication extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        WorkmanagerPlugin.setPluginRegistrantCallback(this);

    }

    @Override
    public void registerWith(PluginRegistry registry) {
        // GeneratedPluginRegistrant.registerWith(registry);

        //add AndroidAlarmManagerPlugin plugin register  if you work with arlarm
        AndroidAlarmManagerPlugin.registerWith(registry.registrarFor("io.flutter.plugins.androidalarmmanager.AndroidAlarmManagerPlugin"));

       //add SharedPreferencesPlugin plugin register  if you work with share preferences
        SharedPreferencesPlugin.registerWith(registry.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));

        // something else...

        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));

        CloudFirestorePlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin"));

        WorkmanagerPlugin.registerWith(registry.registrarFor("be.tramckrijte.workmanager.WorkmanagerPlugin"));
    }
}

The newly created CustomApplication class needs to be specified in the application tag in android manifest file

<application
    android:name="packagename.CustomApplication"

The Android specific files are located at Android project folder, Please check the below screen shot.

enter image description here

like image 150
Sathyajith Avatar answered Sep 22 '22 12:09

Sathyajith