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

后端 未结 1 1756
北海茫月
北海茫月 2021-01-18 02:23

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

相关标签:
1条回答
  • 2021-01-18 03:17

    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.

    0 讨论(0)
提交回复
热议问题