本文介绍了Flutter Workmanager插件在运行任务时无法与任何其他插件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初始化工作管理器并创建任何一个任务后,如果我们在任务执行过程中使用任何插件,将无法识别并抛出错误MissingPluginException(在通道lyokone/location上未找到方法getLocation的实现)

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 bellowMissingPluginException(No implementation found for method getLocation on channel lyokone/location)

实际代码:

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)

推荐答案

如果要从WorkManager的executeTask内部使用其他插件,则;需要创建自定义应用程序类,并需要注册其他插件.如果需要在执行任务中使用WorkManager插件本身,则该插件也需要注册.另外,还需要在AndroidManifest.xml文件中指定此新创建的自定义应用程序.插件的问题链接中提到了此问题,因为无法完全从插件本身.

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"));
    }
}

需要在android清单文件中的application标签中指定新创建的CustomApplication类

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

<application
    android:name="packagename.CustomApplication"

Android专用文件位于Android项目文件夹中,请检查以下屏幕截图.

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

这篇关于Flutter Workmanager插件在运行任务时无法与任何其他插件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:19