All Projects β†’ rmawatson β†’ flutter_isolate

rmawatson / flutter_isolate

Licence: MIT license
Launch an isolate that can use flutter plugins.

Programming Languages

dart
5743 projects
java
68154 projects - #9 most used programming language
objective c
16641 projects - #2 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to flutter isolate

flutter freshchat
The unofficial flutter plugin for Freshchat
Stars: ✭ 31 (-80.25%)
Mutual labels:  flutter-plugin
aestheticDialogs
πŸ“± Flutter Plugin for πŸ’«fluid, 😍beautiful, 🎨custom Dialogs
Stars: ✭ 19 (-87.9%)
Mutual labels:  flutter-plugin
FlutterLoadingGIFs
Loading indicator GIFs. Material and Cupertino (Android and iOS) loading indicators in assorted sizes. Use as placeholders for loading remote image assets. Demo: https://gallery.codelessly.com/flutterwebsites/loadinggifs/
Stars: ✭ 28 (-82.17%)
Mutual labels:  flutter-plugin
acr cloud sdk
🎡 🎢 ACR Cloud SDK For flutter
Stars: ✭ 19 (-87.9%)
Mutual labels:  flutter-plugin
Motion-Tab-Bar
A beautiful animated flutter widget package library. The tab bar will attempt to use your current theme out of the box, however you may want to theme it.
Stars: ✭ 237 (+50.96%)
Mutual labels:  flutter-plugin
plugins
Flutter plugins for Tizen
Stars: ✭ 40 (-74.52%)
Mutual labels:  flutter-plugin
flutter flavorizr
A flutter utility to easily create flavors in your flutter application
Stars: ✭ 260 (+65.61%)
Mutual labels:  flutter-plugin
davinci
A flutter package to convert any widget to an Image.
Stars: ✭ 33 (-78.98%)
Mutual labels:  flutter-plugin
flutter statusbar manager
Flutter StatusBar Manager for iOS & Android
Stars: ✭ 189 (+20.38%)
Mutual labels:  flutter-plugin
liquid button
Liquify your buttons, web demo at website
Stars: ✭ 18 (-88.54%)
Mutual labels:  flutter-plugin
link text
Easy to use text widget for Flutter apps, which converts inlined urls into working, clickable links
Stars: ✭ 20 (-87.26%)
Mutual labels:  flutter-plugin
flutter gesture password
flutter_gesture_password
Stars: ✭ 77 (-50.96%)
Mutual labels:  flutter-plugin
pal-plugin
The Flutter onboarding editor
Stars: ✭ 16 (-89.81%)
Mutual labels:  flutter-plugin
flutter facebook app events
Flutter Plugin for Facebook App Events
Stars: ✭ 88 (-43.95%)
Mutual labels:  flutter-plugin
flutter-google-api-availability
Check the availability of Google Play services on the current device
Stars: ✭ 26 (-83.44%)
Mutual labels:  flutter-plugin
simple gesture detector
Easy to use, reliable and lightweight gesture detector for Flutter apps, exposing simple API for basic gestures
Stars: ✭ 26 (-83.44%)
Mutual labels:  flutter-plugin
flutter custom tabs
A Flutter plugin to use Chrome Custom Tabs.
Stars: ✭ 117 (-25.48%)
Mutual labels:  flutter-plugin
flutter wasm
WebAssembly interpreter for Flutter apps.
Stars: ✭ 22 (-85.99%)
Mutual labels:  flutter-plugin
flutter chameleon
[UNMAINTAINED] Flutter package to show platform **dependent** widgets.
Stars: ✭ 30 (-80.89%)
Mutual labels:  flutter-plugin
E-commerce
A Flutter E-commerce by implementing the Carousel and other flutter components
Stars: ✭ 36 (-77.07%)
Mutual labels:  flutter-plugin

FlutterIsolate

FlutterIsolate allows creation of an Isolate in flutter that is able to use flutter plugins. It creates the necessary platform specific bits (FlutterBackgroundView on android & FlutterEngine on iOS) to enable the platform channels to work inside an isolate.

FlutterIsolate API

Android iOS Description
FlutterIsolate.spawn(entryPoint,message) βœ… βœ… spawns a new FlutterIsolate
FlutterIsolate.pause() βœ… βœ… pauses a running isolate
FlutterIsolate.resume() βœ… βœ… resumed a paused isoalte
FlutterIsolate.kill() βœ… βœ… kills a an isolate
FlutterIsolate.killAll() βœ… βœ… kills all currently running isolates
FlutterIsolate.runningIsolates βœ… βœ… returns the IDs associated with all currently running isolates

Usage

import 'package:flutter_startup/flutter_startup.dart';
import 'package:flutter_isolate/flutter_isolate.dart';

void isolate2(String arg) {
  FlutterStartup.startupReason.then((reason){
    print("Isolate2 $reason");
  });
  Timer.periodic(Duration(seconds:1),(timer)=>print("Timer Running From Isolate 2"));
}

void isolate1(String arg) async  {

  final isolate = await FlutterIsolate.spawn(isolate2, "hello2");

  FlutterStartup.startupReason.then((reason){
    print("Isolate1 $reason");
  });
  Timer.periodic(Duration(seconds:1),(timer)=>print("Timer Running From Isolate 1"));
}

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

  final isolate = await FlutterIsolate.spawn(isolate1, "hello");
  Timer(Duration(seconds:5), (){print("Pausing Isolate 1");isolate.pause();});
  Timer(Duration(seconds:10),(){print("Resuming Isolate 1");isolate.resume();});
  Timer(Duration(seconds:20),(){print("Killing Isolate 1");isolate.kill();});

  runApp(MyApp());
}
...

See example/lib/main.dart for example usage with the flutter_downloader plugin.

It is important to note that the entrypoint must be a top-level function:

void topLevelFunction(Map<String, dynamic> args) {
  // performs work in an isolate
}

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

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    FlutterIsolate.spawn(topLevelFunction, {});
    super.initState();
  }
  Widget build(BuildContext context) {
    return Container();
  }
}

or a static method:

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

class _MyAppState extends State<MyApp> {

  static void topLevelFunction(Map<String, dynamic> args) {
    // performs work in an isolate
  }

  @override
  void initState() {
    FlutterIsolate.spawn(_MyAppState.staticMethod, {});
    super.initState();
  }
  Widget build(BuildContext context) {
    return Container();
  }
}

A class-level method will not work and will throw an Exception:

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

class _MyAppState extends State<MyApp> {

  
  void classMethod(Map<String, dynamic> args) {
    // don't do this!
  }

  @override
  void initState() {
    
    FlutterIsolate.spawn(classMethod, {}); // this will throw NoSuchMethodError: The method 'toRawHandle' was called on null.
    super.initState();
  }
  Widget build(BuildContext context) {
    return Container();
  }
}

Notes

Due to a FlutterIsolate being backed by a platform specific 'view', the event loop will not terminate when there is no more 'user' work left to do and FlutterIsolates will require explict termination with kill().

Additionally this plugin has not been tested with a large range of plugins, only a small subset I have been using such as flutter_notification, flutter_blue and flutter_startup.

Communicating between isolates

To pass data between isolates, a ReceivePort should be created on your (parent) isolate with the corresponding SendPort sent via the spawn method:

void spawnIsolate(SendPort port) {
  port.send("Hello!");
}

void main() {
  var port = ReceivePort();
  port.listen((msg) {
    print("Received message from isolate $msg");
  });
  var isolate = await FlutterIsolate.spawn(spawnIsolate, port.sendPort);

}

Only primitives can be sent via a SendPort - see the SendPort documentation for further details.

Custom plugin registrant

See the example project for a sample implementation using a custom plugin registrant.

iOS

By default, flutter_isolate will register all plugins provided by Flutter's automatically generated GeneratedPluginRegistrant.m file.

If you want to register, e.g. some custom FlutterMethodChannels, you can define a custom registrant:

// Defines a custom plugin registrant, to be used specifically together with FlutterIsolatePlugin
@objc(IsolatePluginRegistrant) class IsolatePluginRegistrant: NSObject {
    @objc static func register(withRegistry registry: FlutterPluginRegistry) {
        // Register channels for Flutter Isolate
        registerMethodChannelABC(bm: registry.registrar(forPlugin: "net.myapp.myChannelABC").messenger())

        // Register default plugins
        GeneratedPluginRegistrant.register(with: registry)
    }
}

// In AppDelegate.swift
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let controller = window.rootViewController as! FlutterViewController

        // Register custom channels for Flutter
        registerMethodChannelABC(bm: controller.binaryMessenger) // <-- the custom method channel

        // Point FlutterIsolatePlugin to use our previously defined custom registrant.
        // The string content must be equal to the plugin registrant class annotation 
        // value: @objc(IsolatePluginRegistrant)
        FlutterIsolatePlugin.isolatePluginRegistrantClassName = "IsolatePluginRegistrant" // <--

        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

Android

Define a custom plugin registrant, to be used specifically together with FlutterIsolatePlugin:

public final class CustomPluginRegistrant {
  public static void registerWith(@NonNull FlutterEngine flutterEngine) {
    flutterEngine.getPlugins().add(... [your plugin goes here]);
  }
}

Create a MainApplication class that sets this custom isolate registrant:

public class MainApplication extends FlutterApplication {
  public MainApplication() {
    FlutterIsolatePlugin.setCustomIsolateRegistrant(CustomPluginRegistrant.class);
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].