dart隔离监听未触发(或工作)

3phpmpom  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(123)

我的班级:

import 'dart:isolate';
import 'dart:ui';

import 'package:flutter_downloader/flutter_downloader.dart';

class SettingsPage extends StatefulWidget with WidgetsBindingObserver {
  SettingsPage({Key? key}) : super(key: key);

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  final ReceivePort _port = ReceivePort();
  int uploadProgress = 0;

  // (-1)
  DownloadTaskStatus uploadStatus = DownloadTaskStatus(-1);
  String identifier = '';

  get downloadsPath =>
      storage.read('downloadsPath') ?? AndroidPathProvider.downloadsPath;

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

    _bindBackgroundIsolate();

    FlutterDownloader.registerCallback(downloadCallback);
  }
 
  @override
  void dispose() {
    _unbindBackgroundIsolate();
    super.dispose();
  }

  void _bindBackgroundIsolate() {
    final isSuccess = IsolateNameServer.registerPortWithName(
        _port.sendPort, 'downloader_send_port');
    simplelog.i('regPort: $isSuccess');

    if (!isSuccess) {
      _unbindBackgroundIsolate();
      _bindBackgroundIsolate();
      return;
    }

    ///Listening for the data is comming other isolataes
    simplelog.i('before listen');

    _port.listen((dynamic data) {
      simplelog.i('in listen');
      final String _id = data[0];
      final DownloadTaskStatus _status = data[1];
      final int _progress = data[2];

      simplelog.i(
        'Callback on UI isolate: '
        'task ($_id) is in status ($_status) and process ($_progress)',
      );

      setState(() {
        uploadProgress = _progress;
        uploadStatus = _status;
        simplelog.i('inside setState');
      });

      simplelog.i(
          'uploadProgress = $uploadProgress \n uploadStatus = $uploadStatus \n uploadStatus.value = ${uploadStatus.value}');
      if (uploadProgress == 100 &&
          uploadStatus == DownloadTaskStatus.complete &&
          uploadStatus.value == 3) {
        try {
          OpenFilex.open('$downloadsPath/${Consts.apkName}');
        } catch (e) {
          simplelog.e('Cannot open file ${Consts.apkName}! \n $e');
        }
      }
    }, onError: (err) {
      simplelog.e('ISOLATE LISTEN ERROR: $err');
    },
        onDone: () {
      simplelog.i('onDone listen');
    });
  }

  void _unbindBackgroundIsolate() {
    IsolateNameServer.removePortNameMapping('downloader_send_port');
  }

  @pragma('vm:entry-point')
  static void downloadCallback(
      String id,
      DownloadTaskStatus status,
      int progress,
      ) {
    simplelog.i('Callback on background isolate: '
        'task ($id) is in status ($status) and progress ($progress)');

    IsolateNameServer.lookupPortByName('downloader_send_port')
        ?.send([id, status, progress]);
  }

dowloadCallback的情况下,一切正常,但在_port.listen()的情况下,我没有得到任何数据(simplelog = print,它没有显示给我,只在simplelog.i('before listen');的情况下工作
在init()方法的寄存器回调中,这个打印机工作良好,并且文件成功下载
我怎样才能解决这个问题?
尝试使用编译器打开和关闭端口

dxpyg8gm

dxpyg8gm1#

此问题由sent()发送的不适当类型参数引起。
1.第一步:

IsolateNameServer.lookupPortByName('downloader_send_port')
        ?.send([id, status.value, progress]);

1.第二步:

_port.listen((dynamic data) {
      ···
      final DownloadTaskStatus _status = DownloadTaskStatus(data[1] as int);
      ···
   }

你可以从作者的GitHub repo中看到更详细的例子。

相关问题