A Flutter package which allows you to work with MethodChannels in Isolate.

Overview

License: MIT Pub

This plugin Combines Isolate and MethodChannel.
In other words it provides a way to use flutter plugins in Isolate or just work with user friendly API for Isolate.

Features

  • Create an Isolate.
  • Communicate with Isolate without extra code.
  • Use Method Channels in Isolate.
  • Supports all flutter platforms.

โŒ๏ธ It doesn't create Isolate alternative (aka service worker) in web. So all code will work in single (main) Isolate.

Index

Usage

Create and maintain Isolate

Create

CombineIsolate is just a representation of Isolate so when you create a CombineIsolate Isolate will be created under the hood except web platform.

To create a new CombineIsolate you just need to call Combine().spawn(entryPointFunction). entryPointFunction is a function which will be called in Isolate.

CombineIsolate combineIsolate = await Combine().spawn((context) {
  print("Hello from Isolate!!!");
});

Listen to errors

To listen to errors you can use CombineIsolate.errors getter which returns stream with errors from isolate.

CombineIsolate combineIsolate;
combineIsolate.errors.listen(print); // Listen for errors.

Kill

You can use CombineIsolate.kill method to kill CombineIsolate.

CombineIsolate combineIsolate;
combineIsolate.kill(); // Kill Isolate.

Communicate with Isolate

IsolateContext

Do you remember context argument in entryPointFunction? Let's take a closer look at it.

IsolateContext holds an argument, passed while you spawn Isolate and IsolateMessenger which is used to communicate with original Isolate.

Pass arguments

To provide argument just pass it to the spawn function.

Combine().spawn(
  (context) {
    final argument = context.argument as String;
    print(argument); // Print: This is my argument
  },
  argument: "This is my argument",
);

Chat with Isolate

To chat with Isolate you can use IsolateMessenger. It has messages getter with stream of messages from Isolate and send method which sends message to Isolate.

In the created isolate you can get IsolateMessenger from IsolateContext.messenger. Another IsolateMessenger can be found in CombineIsolate.

CombineIsolate combineIsolate = await Combine().spawn((context) {
  context.messenger
    ..messages.listen(
      (event) => print("Message from Main Isolate: $event"),
    )
    ..send("Hello from Combine Isolate!");
});

combineIsolate.messenger.messages.listen(
  (event) {
    print("Message from Combine Isolate: $event");
    combineIsolate.messenger.send("Hello from Main Isolate!");
  },
);

This code will give the following output:

Message from Combine Isolate: Hello from Combine Isolate!
Message from Main Isolate: Hello from Main Isolate!

Dealing with MethodChannels

Configuration

Everything is already configured to work with MethodChannels so you can just use them!

Combine().spawn((context) async {
  final textFromTestAsset = await rootBundle.loadString("assets/test.txt");
  print("Text from test asset: $textFromTestAsset");
  // Print: Text from test asset: Asset is loaded!
});

Explanation:

  • the point it that rootBundle uses BinaryMessenger (low level MethodChannel)
  • let's assume that file in assets/test.txt exists and contains Asset is loaded! text

Limitations

Everything will work fine while MethodChannel.invokeMethod or BinaryMessenger.send methods are used by you or your plugin.

However if MethodChannel.setMethodCallHandler or BinaryMessenger.handlePlatformMessage are used by you or your plugin you may notice that these methods are not working. This may happen if you didn't send any data to the platform from this Isolate.

Why? In short the reason is that plugin just sends all messages from known [method] channels in Main Isolate to the Combine Isolate. However [method] channel becomes known when you send anything to it. The good news is when you want to receive messages from channel using MethodChannel.setMethodCallHandler or BinaryMessenger.handlePlatformMessage methods almost always firstly you send some data to this channel so it is very unlikely that you will face this problem.

Additional information

Limitation may be fixed by some cool hacks and I'll try to do it later.

Also as you might have noticed this package is in beta version.
So firstly it means that API may be changed.
Secondly it means that I need to do a lot of things so I need your help. If you like this package please like and star it! If you have something to say please create an issue!
I want to know that this package can help someone. It will give me the strength to continue working on it :)

You might also like...

This is flutter package for creating a gender selection widget which allows users to choose a gender with cool animations

This is flutter package for creating a gender selection widget which allows users to choose a gender with cool animations

gender_selection A Flutter package for gender selection. It is an aweome gender selection widget with cool gradients and animation effects Demo Instal

Apr 8, 2022

CircularProfileAvatar is a Flutter package which allows developers to implement circular profile avatar

CircularProfileAvatar is a Flutter package which allows developers to implement circular profile avatar

CircularProfileAvatar is a Flutter package which allows developers to implement circular profile avatar with border, overlay, initialsText and many other awesome features, which simplifies developers job. It is an alternative to Flutter's CircleAvatar Widget.

Oct 5, 2022

This package allows you to draw dotted lines with Flutter.

This package allows you to draw dotted lines with Flutter.

About This package allows you to draw dotted lines with Flutter. Usage Parameter Default Description direction Axis.horizontal The direction of the en

Nov 16, 2022

A flutter deskstop package that allows you to drag the native file into app support.

A flutter deskstop package that allows you to drag the native file into app support.

FileDragAndDrop A flutter deskstop package that allows you to drag the native file into app support. Platform Support Now only support on macOS, if an

Oct 24, 2022

BubbleShowcase is a small but power flutter package that allows you to highlight specific parts of your app to explain them to the user or to showcase your app new features.

BubbleShowcase BubbleShowcase is a small but powerful flutter package that allows you to highlight specific parts of your app (to explain them to the

Oct 26, 2022

A Flutter package allows you to Showcase/Highlight your widgets step by step.๐Ÿ‘Œ๐Ÿ”๐ŸŽ‰

A Flutter package allows you to Showcase/Highlight your widgets step by step.๐Ÿ‘Œ๐Ÿ”๐ŸŽ‰

ShowCaseView A Flutter package allows you to Showcase/Highlight your widgets step by step. Preview Installing Add dependency to pubspec.yaml Get the l

Jan 2, 2023

A Flutter package allows you to obtain your public IP address

IP Address This package allows you to obtain your public IP address and many other information about it Features You can get the public IP address of

Nov 30, 2022

This package allows you to scroll/select the value directly from the dropdown with less effort and time.

Direct Select This package allows you to scroll/select the value directly from the dropdown with less effort and time. Inspired by Virgil Pana shot Sa

Nov 25, 2022

This project is a NGO which let you donate anything or even let you ask for help to people.

ngo_app This app is written in flutter using dart language. Getting Started This project is a NGO which let you donate anything or even let you ask fo

May 8, 2022
Comments
  • Feat: Add `CombineWorker.newInstance` factory

    Feat: Add `CombineWorker.newInstance` factory

    Add initializer parameter to the CombineWorker.initialize method

    PR Checklist

    • [x] Code is 100% covered.
    • [x] I updated/added relevant documentation.
    • [x] All existing and new tests are passing.
    • [x] Changelog is updated.
    • [x] Version is pumped
    enhancement 
    opened by Maksimka101 1
  • Feature/combine-worker

    Feature/combine-worker

    Add CombineWorker and update Combine api

    PR Checklist

    • [x] Code is 100% covered.
    • [x] I updated/added relevant documentation.
    • [x] All existing and new tests are passing.
    • [x] Changelog is updated.
    • [x] Version is pumped
    enhancement 
    opened by Maksimka101 1
  • Expose api

    Expose api

    *Replace this paragraph with a description of what this PR is changing or adding, and why. *

    PR Checklist

    • [x] Code is 100% covered.
    • [x] I updated/added relevant documentation.
    • [x] All existing and new tests are passing.
    • [x] Changelog is updated.
    • [x] Version is pumped.
    opened by Maksimka101 1
Owner
Zemlaynikin Max
Zemlaynikin Max
Dart package that conveniently wraps an isolate, ports and state for easy isolates.

Isolate Agents Description Isolate Agents adds a new class, Agent, which is a proper implementation of the Actor model for Dart. Where Isolates have n

null 65 Jan 2, 2023
An isolated worker for Flutter (Isolate) and Web (Web Worker). Behaves almost the same as the compute function, except it is not a one-off worker.

A singleton isolated worker for all platforms. On most platforms, it uses Flutter's Isolate, except on the web, since Isolate is not available, it use

Iandi Santulus 30 Nov 11, 2022
Isolate helper for flutter and dart.

Isolation Overview The package simplifies the creation and interaction between isolates. It encapsulates the entire boilerplate, leaving the developer

Plague Fox 15 Oct 29, 2022
Aplicativo de teste que roda em background timer atual utilizando Isolate.

# isolate_app A new Flutter project. ## Getting Started This project is a starting point for a Flutter application. A few resources to get you sta

Isaque Santos Paixรฃo 1 Jun 20, 2022
Learn how to build a multi-step form flow and how to use bloc to effectively isolate the presentation layer from the business logic layer.

Multi-page Form Flow Learn how to build a multi-step form flow and how to use bloc to effectively isolate the presentation layer from the business log

Sandip Pramanik 15 Dec 19, 2022
This a library to simplify isolate thread communication.

This a library to simplify islate thread communication. It abstracts the data transfer between islate and the main thread into a simple channel, and t

ๅดๆฅš่กก 3 Oct 31, 2022
This is a quizz app which is work with firebase.

guess_what A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if th

null 2 May 25, 2022
This package adds CustomRefreshIndicator widget that allows you to create whatever indicator you want.

Custom Refresh Indicator A flutter package that allows you to easily create a custom refresh indicator widget. TLDR; ONLINE DEMO! QUICK START CustomRe

Kamil Klyta 315 Dec 16, 2022
A Flutter plugin which allows you to execute code in the background on Android and iOS.

Flutter Workmanager Flutter WorkManager is a wrapper around Android's WorkManager, iOS' performFetchWithCompletionHandler and iOS BGAppRefreshTask, ef

Flutter Community 699 Jan 5, 2023
A user-friendly money management app which allows you to keep track of transactions seamlessly

See the first version of 'Mvelopes' - Money Management Application which I completed as my first project and published on Play Store. 'Mvelopes' is a user-friendly money management app which allows you to keep track of transactions seamlessly. - Features - Technology โ€ข Reminder โ€ข Flutter โ€ข Notification โ€ข Hive โ€ข Manage income & expenses

Mushthak 16 Dec 8, 2022