简体   繁体   中英

flutter_downloader plugin is not working on existing project

I want to implement the download functionality to my existing app but it throws an exception and I cannot figure out why.

I freshly created a new flutter project and installed the flutter_downloader plugin and it works fine but when I implement the feature in the same way in my existing application, it throws me an error.

The stack trace is here:

2019-11-02 13:11:44.655 13280-13280/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 13280
java.lang.IllegalStateException: registrar.activity() must not be null
    at co.paystack.flutterpaystack.FlutterPaystackPlugin$Companion.registerWith(FlutterPaystackPlugin.kt:18)
    at co.paystack.flutterpaystack.FlutterPaystackPlugin.registerWith(Unknown Source:2)
    at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:30)
    at com.example.app.FlutterCustomApp.registerWith(FlutterCustomApp.java:10)
    at vn.hunghd.flutterdownloader.DownloadWorker.startBackgroundIsolate(DownloadWorker.java:124)
    at vn.hunghd.flutterdownloader.DownloadWorker.access$000(DownloadWorker.java:59)
    at vn.hunghd.flutterdownloader.DownloadWorker$1.run(DownloadWorker.java:97)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

This is my custom application class:

public class FlutterCustomApp extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {
@Override
public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
}

}

And I specified the class name in the AndroidManifest.xml file as well.

But when I initiate a download task, above exception is thrown and the app is crashed. I still couldn't figure out why this is happening.

  • compileSdkVersion: 28
  • compileSdkVersion: 21
  • targetSdkVersion: 28
  • gradle version: 3.5.1

Since flutter engine is required to communicate with the native android that depends on the android lifecycle (if you take a look the FlutterActivity ), the registry of the plugin should start register on the activity instead of the application class.

You should call the plugin registration on the first Splash Activity or BaseActivity onCreate, if there is no other specific concern or reason like JNI initialization or Flutter engine related config changes:

class MainActivity: FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM