Flutter workmanager 插件在运行任务时无法与任何其他插件一起使用 [英] Flutter workmanager plugin doesn't work with any other plugin when running task

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

问题描述

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

实际代码:

Workmanager.executeTask((task, inputData) async {位置 locationObject = Location();locationObject.getLocation();打印(位置对象);返回 Future.value(true);}

基本上在工作管理器的任务中使用的任何其他插件似乎都无法识别.

我错过了什么,我需要重新注册所有插件吗?

I/flutter (16120):位置权限有误

I/flutter (16120): MissingPluginException(在频道lyokone/location上找不到方法serviceEnabled的实现)

解决方案

如果想使用WorkManager内部的其他插件executeTask then;需要创建自定义应用程序类,并需要注册其他插件.如果在执行任务中需要使用 WorkManager 插件本身,则该插件也需要注册.此外,需要在 AndroidManifest.xml 文件中指定这个新创建的自定义应用程序.插件的

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)

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆