title: How to make Flutter Workmanager plugin and Location plugin work together
body: <p><strong>1. Problem description</strong></p>
  
  <p>My goal is to build a Flutter app that gets periodic location updates using <a href="https://pub.dev/packages/workmanager" rel="noreferrer">this workmanager plugin</a> and using <a href="https://pub.dev/packages/location" rel="noreferrer">this location plugin</a>. But I can't get the Location plugin to be loaded properly when my Workmanager callback fires. I get this error:</p>
  
  <pre><code>MissingPluginException(No implementation found for method getLocation on channel lyokone/location)
  </code></pre>
  
  <p>So basically, the problem is that when the Workmanager plugin tries to run dart code, it doesn't load up the Location plugin. </p>
  
  <p><strong>2. Other resources I have researched</strong></p>
  
  <p>I found others facing the same issue, <a href="https://stackoverflow.com/questions/58194516/flutter-workmanager-plugin-doesnt-work-with-any-other-plugin-when-running-task/60207423?noredirect=1#comment106582814_60207423">here</a>, <a href="https://github.com/vrtdev/flutter_workmanager/issues/6" rel="noreferrer">here</a>, and <a href="https://stackoverflow.com/questions/59478280/how-run-background-process-with-the-work-manager-in-flutter">here</a>.</p>
  
  <p>As far as I understand, the solution provided to these questions boils down to: create a file named <em>CustomApplication.java</em>, which extends <em>FlutterApplication</em>, and which registers your plugin(s). And then register the CustomApplication.java file inside you AndoidManifest.xml file.</p>
  
  <p><strong>3. My code thus far</strong></p>
  
  <p>I have tried to make a bare-minimum app that implements the features I require:</p>
  
  <ol>
  <li>I implemented Workmanager plugin (works fine)</li>
  <li>I implemented Location plugin (works fine)</li>
  <li>I attempted to combine these features (does not work)</li>
  </ol>
  
  <p>To see exactly what I have done at each step, please look here: <a href="https://gitlab.com/tomoerlemans/workmanager_with_location/-/commits/master" rel="noreferrer">https://gitlab.com/tomoerlemans/workmanager_with_location/-/commits/master</a>. (This repository can also be used to quickly replicate the issue).</p>
  
  <p>The relevant code files are as follows:</p>
  
  <p><strong>main.dart</strong></p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:workmanager/workmanager.dart';
  import 'package:location/location.dart';
  
  void main() {
    WidgetsFlutterBinding.ensureInitialized();
    Workmanager.initialize(callbackDispatcher, isInDebugMode: true);
    runApp(MyApp());
  }
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: MyHomePage(),
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              RaisedButton(
                onPressed: () {
                  Workmanager.registerPeriodicTask(
                      "1", "simpleTask");
                },
                child: Text("Start workmanager"),
              ),
              RaisedButton(
                onPressed: () {
                  getLocation();
                },
                child: Text("Get current location"),
              ),
            ],
          ),
        ),
      );
    }
  }
  
  void callbackDispatcher() {
    Workmanager.executeTask((task, inputData) {
      print("Native called background task at ${DateTime.now().toString()}");
      getLocation();
      return Future.value(true);
    });
  }
  
  void getLocation() async {
    LocationData currentLocation;
    var location = new Location();
    try {
      currentLocation = await location.getLocation();
    } on Exception catch (e) {
      print("Error obtaining location: $e");
      currentLocation = null;
    }
    print("Location altitude: ${currentLocation.altitude}");
    print("Location longitude: ${currentLocation.longitude}");
  }
  </code></pre>
  
  <p><strong>pubspec.yaml</strong></p>
  
  <pre><code>name: background_location
  description: A new Flutter project.
  
  version: 1.0.0+1
  
  environment:
    sdk: "&gt;=2.1.0 &lt;3.0.0"
  
  dependencies:
    flutter:
      sdk: flutter
  
    cupertino_icons: ^0.1.2
    workmanager: ^0.2.0
    location: ^2.3.5
  
  
  dev_dependencies:
    flutter_test:
      sdk: flutter
  
  flutter:
    uses-material-design: true
  </code></pre>
  
  <p><strong>CustomApplication.java</strong></p>
  
  <pre><code>package io.flutter.plugins;
  
  import io.flutter.app.FlutterApplication;
  import io.flutter.plugin.common.PluginRegistry;
  import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
  import io.flutter.plugins.GeneratedPluginRegistrant;
  import be.tramckrijte.workmanager.WorkmanagerPlugin;
  import com.lyokone.location.LocationPlugin;
  
  public class CustomApplication extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {
      @Override
      public void onCreate() {
          super.onCreate();
          WorkmanagerPlugin.setPluginRegistrantCallback(this);
  
      }
      @Override
      public void registerWith(PluginRegistry registry) {
          WorkmanagerPlugin.registerWith(registry.registrarFor("be.tramckrijte.workmanager.WorkmanagerPlugin"));
          LocationPlugin.registerWith(registry.registrarFor("com.lyokone.location.LocationPlugin"));
      }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.background_location"&gt;        
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
      &lt;application
          android:name="io.flutter.plugins.CustomApplication"
          android:label="background_location"
          android:icon="@mipmap/ic_launcher"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:launchMode="singleTop"
              android:theme="@style/LaunchTheme"
              android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
              android:hardwareAccelerated="true"
              android:windowSoftInputMode="adjustResize"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN"/&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;meta-data
              android:name="flutterEmbedding"
              android:value="2" /&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Finally, I am running the following versions of Dart/Flutter/etc:</p>
  
  <pre><code>Flutter 1.12.13+hotfix.5 • channel stable • https://github.com/flutter/flutter.git
  Framework • revision 27321ebbad (10 weeks ago) • 2019-12-10 18:15:01 -0800
  Engine • revision 2994f7e1e6
  Tools • Dart 2.7.0
  </code></pre>