isolate_channel 0.1.1 copy "isolate_channel: ^0.1.1" to clipboard
isolate_channel: ^0.1.1 copied to clipboard

Communication channels for isolates based on Flutter's plugin channels

example/isolate_channel_example.dart

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

import 'package:isolate_channel/isolate_channel.dart';

void main() async {
  final connection = await spawnIsolate(isolateEntryPoint);

  final methodChannel = IsolateMethodChannel('method_channel', connection);
  final eventChannel = IsolateEventChannel('event_channel', connection);

  final result = await methodChannel.invokeMethod('example_method', 'Hello');
  print(result);

  final stream = eventChannel.receiveBroadcastStream('Hello');
  final subscription = stream.listen(print);
  subscription.onError(print);

  final completer = Completer<void>();
  subscription.onDone(completer.complete);
  await completer.future;

  connection.close();
}

void isolateEntryPoint(SendPort send) {
  final connection = setupIsolate(send);

  final methodChannel = IsolateMethodChannel('method_channel', connection);
  methodChannel.setMethodCallHandler((call) {
    switch (call.method) {
      case 'example_method':
        print(call.arguments);
        return 'World!';
      default:
        return IsolateException.notImplemented(call.method);
    }
  });

  final eventChannel = IsolateEventChannel('event_channel', connection);
  eventChannel.setStreamHandler(
    IsolateStreamHandler.inline(
      onListen: (arguments, sink) {
        sink.success(arguments);
        sink.error(
          code: 'error',
          message: 'Something went wrong',
          details: 1234,
        );
        sink.endOfStream();
      },
      onCancel: (arguments) => print('Canceled: $arguments'),
    ),
  );
}
4
likes
0
points
2.46k
downloads

Publisher

verified publisheriodesignteam.com

Weekly Downloads

Communication channels for isolates based on Flutter's plugin channels

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on isolate_channel