使用Flutter Downloader插件下载后,应用程序会关闭。

15

我使用flutter_downloader包。在下载一些文件后,我的应用程序会自动关闭并与Android Studio断开连接。请问有人能帮我找到解决方案吗?

    final status = await Permission.storage.request();
            if (status.isGranted) {
              await downloadPDF();
            } 

downloadPDF() async {
    final externalDir = await getExternalStorageDirectory();
    taskId = await FlutterDownloader.enqueue(
      url: pdfURL,
      savedDir: externalDir.path,
      fileName: "Flamingo Order Details",
      showNotification: true,
      openFileFromNotification: true,
    );
  }

这是我的控制台错误:

I/flutter (15913): Fatal: could not find callback
F/libc    (15913): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 15956 (1.raster), pid 15913 (le.order_system)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'motorola/chef/chef_sprout:10/QPTS30.61-18-16-8/03acd:user/release-keys'
Revision: 'pvt'
ABI: 'arm64'
Timestamp: 2021-04-23 16:39:38+0530
pid: 15913, tid: 15956, name: 1.raster  >>> com.example.order_system <<<
uid: 10870
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
Cause: null pointer dereference
    x0  000000741e5a6478  x1  00000074841cfa00  x2  0000000000000001  x3  0000000000000000
    x4  0000000000000000  x5  00000000ffffffff  x6  00000000ffffffff  x7  0000000b96c6a75c
    x8  0000000080000081  x9  658adf78f7e836ee  x10 0000000000000000  x11 0000000000000000
    x12 0000000000000001  x13 000000747b19fe80  x14 0000007489a0a280  x15 0000000000000000
    x16 000000747b19b050  x17 0000007515c9787c  x18 000000741d05e000  x19 000000741e5a6478
    x20 00000074841cfa00  x21 0000000000000001  x22 0000000000000000  x23 00000074843db380
    x24 000000741e5a7020  x25 000000741e5a7020  x26 0000000000000000  x27 0000000000000001
    x28 0000000000000043  x29 000000741e5a6450
    sp  000000741e5a6420  lr  000000747b013f84  pc  000000747b01c378
backtrace:
      #00 pc 00000000001d8378  /vendor/lib64/egl/libGLESv2_adreno.so (BuildId: 22cc95e0051ae85072c405eeeeeb312d)
      #01 pc 00000000001cff80  /vendor/lib64/egl/libGLESv2_adreno.so (BuildId: 22cc95e0051ae85072c405eeeeeb312d)
      #02 pc 00000000000207b0  /system/lib64/libEGL.so (android::eglSwapBuffersWithDamageKHRImpl(void*, void*, int*, int)+316) (BuildId: 248ba7f2d80e7bb9952a20e1c3493c86)
      #03 pc 000000000001d0a8  /system/lib64/libEGL.so (eglSwapBuffers+80) (BuildId: 248ba7f2d80e7bb9952a20e1c3493c86)
      #04 pc 00000000012ec528  /data/app/com.example.order_system-8XYsushVsEZPOltQ3k8npA==/lib/arm64/libflutter.so (BuildId: e14966237eb013b063fed6484195268f7398b594)
Lost connection to device.

你正在使用任何 API 吗? - Ujjwal Raijada
是的,但我认为这不会有影响,因为文件下载成功后应用程序就会关闭。 - Harsh Sureja
我认为你已经更改了堆栈跟踪。之前的错误是由于糟糕的API引起的。现在出现了“致命信号11(SIGSEGV)”的错误。这种情况发生在应用程序访问其地址空间外的内存时。请查看https://dev59.com/iuo6XIcBkEYKwwoYLhbh以获取更多信息。 - Ujjwal Raijada
1个回答

25

也许现在已经晚了,但这可能会帮助其他人。最近我遇到了这个错误并解决了它。您的 UI 在主隔离区中呈现,而下载事件来自后台隔离区。由于回调中的代码在后台隔离区运行,因此您必须处理两个隔离区之间的通信。通常,需要进行通信才能在主 UI 中显示下载进度。实施以下代码以处理通信:

import 'dart:isolate';
import 'dart:ui'; // You need to import these 2 libraries besides another libraries to work with this code

final 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]);
  }


void _download(String url) async {
    final status = await Permission.storage.request();

    if(status.isGranted) {
      final externalDir = await getExternalStorageDirectory();

      final id = await FlutterDownloader.enqueue(
          url: url,
          savedDir: externalDir!.path,
        showNotification: true,
        openFileFromNotification: true,
      );
    } else {
      print('Permission Denied');
    }
  }

在你的按钮中调用_download(url)函数,你就准备好了。

注意:我的应用程序中使用了空安全,因此在这行代码中我使用了null-aware运算符:

final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port')!;

1
每个使用下载功能的页面都需要进行这些过程吗?还是我可以将其设为全局? - RahulZx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接