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

1

8

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)

Flatware answered 2/10, 2019 at 2:33 Comment(2)
Do you have any addition information about the plugins you're using and some sample code?Rapping
Did you solve it? I am facing the same problem.Tachymetry
L
2

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

Lissy answered 13/2, 2020 at 12:3 Comment(5)
Could you please provide some more details on where to place this file, how to register it inside AndroidManifest.xml, and other necessary steps. Because I keep getting stuck.Piccadilly
I tried following your instructions and I added the dependencies you used such as 'shared_preferences: ^0.5.6+1', to my pubspec.yaml. I also enabled multiDex inside my app/build.gradle file. But I keep on getting errors/the app won't built. I need to replace "android:name="io.flutter.app.FlutterApplication" in AndroidManifest.xml right? And where do I find my package name? (I mean I tried doing a lot of stuff but it still won't work so somewhere I am still doing something wrong).Piccadilly
What is the build error. Package name you can find at the top of the CustomApplication class file.Lissy
I finally managed to get it to build. I put "package io.flutter.plugins;" at the top of my CustomApplication.java file. And "android:name="io.flutter.plugins.CustomApplication" inside AndroidManifest.xml. And I added "import com.lyokone.location.LocationPlugin;" and "LocationPlugin.registerWith(registry.registrarFor("com.lyokone.location.LocationPlugin"));" inside CustomApplication.java. However... I still get the " MissingPluginException(No implementation found for method getLocation on channel lyokone/location)" error.Piccadilly
This is getting too chaotic for comments IMO, I have tried to state my question more clearly here: #60262925Piccadilly

© 2022 - 2024 — McMap. All rights reserved.