Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running on isolate instance has not been initialized on windows #88

Open
alexaccaci opened this issue Feb 1, 2023 · 1 comment
Open

Comments

@alexaccaci
Copy link

Using version 0.4.0 quick_usb init doen't work on isolate.
I'haven't this problem using the prevoius version 0.3.1.
I've attached an example.
Best Regards
Alex

import 'dart:async';
import 'dart:isolate';

import 'package:flutter/material.dart';
import 'package:quick_usb/quick_usb.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

runApp(MyHome());
}

class MyHome extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: MyApp(),
),
);
}
}

class MyApp extends StatefulWidget {
@OverRide
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
@OverRide
void initState() {
super.initState();
init();
}

Future init() async {
UsbService.usbRecvPort = ReceivePort();
await Isolate.spawn(UsbService.loopUsb, UsbService.usbRecvPort!.sendPort);
}

@OverRide
Widget build(BuildContext context) {
return _buildColumn();
}

void log(String info) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(info)));
}

Widget _buildColumn() {
return Column(
children: [
_init_exit(),
_getDeviceList(),
],
);
}

Widget _init_exit() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
child: Text('init'),
onPressed: () async {
var init = await QuickUsb.init();
log('init $init');
},
),
ElevatedButton(
child: Text('exit'),
onPressed: () async {
await QuickUsb.exit();
log('exit');
},
),
],
);
}

List? _deviceList;

Widget _getDeviceList() {
return ElevatedButton(
child: Text('getDeviceList'),
onPressed: () async {
_deviceList = await QuickUsb.getDeviceList();
log('deviceList $_deviceList');
},
);
}
}

class UsbService {
static SendPort? usbSendPort;
static ReceivePort? usbRecvPort;

static void loopUsb(SendPort sendPort) async {
ReceivePort p = ReceivePort();
sendPort.send(p.sendPort);

while (true) {
  await Future.delayed(const Duration(seconds: 1));
  try {
    var init = await QuickUsb.init();
    print('init $init');

    var list = await QuickUsb.getDeviceList();
    print(list);

    await QuickUsb.exit();
  } catch (e) {
    print(e);
  }
}

}
}

@alexaccaci
Copy link
Author

I have resolved the problem using the following article:
https://medium.com/flutter/introducing-background-isolate-channels-7a299609cad8

But there is another small error in your code that needs to correct:

class QuickUsbWindows extends _QuickUsbDesktop {
// For example/.dart_tool/flutter_build/generated_main.dart
static registerWith() {
QuickUsbPlatform.instance = QuickUsbMacos();
_libusb = Libusb(DynamicLibrary.open('libusb-1.0.23.dll'));
}
}

has to be changed with:

class QuickUsbWindows extends _QuickUsbDesktop {
// For example/.dart_tool/flutter_build/generated_main.dart
static registerWith() {
QuickUsbPlatform.instance = QuickUsbWindows();//not QuickUsbMacos!!!
_libusb = Libusb(DynamicLibrary.open('libusb-1.0.23.dll'));
}
}

Best regards
Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant