引用插件isolate_manager
import 'package:isolate_manager/isolate_manager.dart';
class MixIsolater {
IsolateManager isolateManager;
Function(dynamic)? onMessageCallBack;
MixIsolater(this.isolateManager, this.onMessageCallBack) {
isolateManager.onMessage.listen((event) {
onMessageCallBack?.call(event);
});
}
Future<void> satrt() async {
await isolateManager.start();
}
sendMessage(dynamic params) {
isolateManager.sendMessage(params);
}
onClose() {
isolateManager.stop();
}
Stream get onMessage {
return isolateManager.onMessage;
}
}
具体使用方法
// 声明
/// 这个方法必须是
final IsolateManager _importManager = IsolateManager.create(
importExcel,
concurrent: 2,
);
late MixIsolater _isolater;
// 初始时候使用
init(){
// 定义单线程
_isolater = MixIsolater(_importManager, _onImportCallback);
_isolater.satrt();
}
// 触发使用,传递参数
onMethod(){
_isolater.sendMessage("params");
}
// 监听回调结果函数
_onImportCallback(dynamic jsonStr) {
}
// 导入excel文件
@pragma('vm:entry-point')
Future<String?> importExcel(dynamic message) async {
String? excelJson = await ExcelToJson().convert();
return excelJson;
}
网友评论