Calling a UI method from Isolate listen method in Flutter throws exception

AVEbrahimi

I tried to use ovprogresshud (https://pub.dev/packages/ovprogresshud) to show download progress. I used flutter_downloader (https://pub.dev/packages/flutter_downloader) for download.

I try to update progress via an Isolate, but receive errors. (If I call Progresshud.showWithStatusdirectly in my code, say before download, it works)

My code:

ReceivePort _port = ReceivePort();
...

IsolateNameServer.registerPortWithName(
        _port.sendPort, 'downloader_send_port');
_port.listen(
      (dynamic data) {
        String id = data[0];
        DownloadTaskStatus status = data[1];
        int progress = data[2];
        if (status == DownloadTaskStatus.complete) {
        } else if (status == DownloadTaskStatus.running) {
          Progresshud.showWithStatus("%$progress Downloaded");
        }
      },
      onDone: () {
        checkIfDictionaryUnzipped(DBFilePath);
      },
      onError: (error) {},
    );

The error I receive:

Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.view.ViewParent android.view.ViewGroup.getParent()' on a null object reference, null)
E/flutter (29114): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (29114): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter (29114): <asynchronous suspension>
E/flutter (29114): #2      Progresshud.showWithStatus (package:ovprogresshud/progresshud.dart:18:27)
E/flutter (29114): <asynchronous suspension>
....
Abhay Koradiya

check important note here

Important note: your UI is rendered in the main isolate, while download events come from a background isolate (in other words, codes in callback are run in the background isolate), so you have to handle the communication between two isolates.

Check Example,

ReceivePort _port = ReceivePort();

@override
void initState() {
    super.initState();

    IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
    _port.listen((dynamic data) {
        String id = data[0];
        DownloadTaskStatus status = data[1];
        int progress = data[2];
        setState((){ });
    });

    FlutterDownloader.registerCallback(downloadCallback);
}

@override
void dispose() {
    IsolateNameServer.removePortNameMapping('downloader_send_port');
    super.dispose();
}

static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
    final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port');
    send.send([id, status, progress]);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Java code from Flutter throws Unhandled Exception: MissingPluginException(No implementation found for method...)

From Dev

Calling dynamic callback function from an async method in the ScriptingObject of the BrowserControl throws Exception

From Dev

Send platform message to Android method from background flutter isolate

From Dev

Calling Dispose method inside constructor which throws an exception or behaves unexpectedly

From Dev

Calling a class method with scoped coroutine in Kotlin/Native on Linux throws exception

From Dev

Calling interface method in razor page throws unhandled exception

From Java

throws Exception in a method with Kotlin

From Dev

NullPointer Exception when calling a method from onResume

From Dev

"Method must return a result" when calling another method that only throws an exception

From Dev

Calling a event method on UI

From Dev

Add a method to Number throws exception

From Java

Determining which method throws exception

From Dev

getMethod throws method not found exception

From Dev

CollectionView method 'referenceSizeForHeaderInSection' throws exception

From Dev

TransactionPrint Method of POSPrinter throws Exception

From Dev

Java method contains another method that throws exception

From Dev

Flutter update UI from native method call by using setState() method

From Dev

MissingPluginException(No implementation found for method DocumentReference) on Send data to firestore from flutter isolate

From Java

Calling a method from another method

From Dev

NoSuchMethod Exception when calling a method

From Dev

calling catch block from a method when an exception occured in try block

From Java

What happens if a method throws an exception that was not specified in the method declaration with "throws"

From Dev

Calling a method and throws exception saying i'm passing in too many values... but i'm not

From Dev

Flutter calling a method inside the build method

From Dev

Calling a method from a superclass

From Dev

Calling method from view

From Java

Calling method from constructor

From Dev

Calling a method from arraylist

From Dev

Calling a method from a class

Related Related

  1. 1

    Calling Java code from Flutter throws Unhandled Exception: MissingPluginException(No implementation found for method...)

  2. 2

    Calling dynamic callback function from an async method in the ScriptingObject of the BrowserControl throws Exception

  3. 3

    Send platform message to Android method from background flutter isolate

  4. 4

    Calling Dispose method inside constructor which throws an exception or behaves unexpectedly

  5. 5

    Calling a class method with scoped coroutine in Kotlin/Native on Linux throws exception

  6. 6

    Calling interface method in razor page throws unhandled exception

  7. 7

    throws Exception in a method with Kotlin

  8. 8

    NullPointer Exception when calling a method from onResume

  9. 9

    "Method must return a result" when calling another method that only throws an exception

  10. 10

    Calling a event method on UI

  11. 11

    Add a method to Number throws exception

  12. 12

    Determining which method throws exception

  13. 13

    getMethod throws method not found exception

  14. 14

    CollectionView method 'referenceSizeForHeaderInSection' throws exception

  15. 15

    TransactionPrint Method of POSPrinter throws Exception

  16. 16

    Java method contains another method that throws exception

  17. 17

    Flutter update UI from native method call by using setState() method

  18. 18

    MissingPluginException(No implementation found for method DocumentReference) on Send data to firestore from flutter isolate

  19. 19

    Calling a method from another method

  20. 20

    NoSuchMethod Exception when calling a method

  21. 21

    calling catch block from a method when an exception occured in try block

  22. 22

    What happens if a method throws an exception that was not specified in the method declaration with "throws"

  23. 23

    Calling a method and throws exception saying i'm passing in too many values... but i'm not

  24. 24

    Flutter calling a method inside the build method

  25. 25

    Calling a method from a superclass

  26. 26

    Calling method from view

  27. 27

    Calling method from constructor

  28. 28

    Calling a method from arraylist

  29. 29

    Calling a method from a class

HotTag

Archive