搜索插件、代码仓与 APP 分类

插件详情v2.1.0

flutter_isolate

FlutterIsolate 提供了一种在 Flutter 中启动 Dart Isolate 的方法,该方法可与 Flutter 插件配合使用。

AndroidiOS
近 30 天下载
1.8万
喜欢数
482
Pub 分数
150

安装插件

flutter pub add flutter_isolate

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

FlutterIsolate 允许在 Flutter 应用中创建和管理 Dart 隔离,从而在不阻塞主 UI 线程的情况下并发执行 CPU 密集型任务。它专门设计用于与 Flutter 插件无缝协作,确保基于隔离的操作可以安全地与平台特定功能交互。该包通过利用 Dart 的隔离模型简化了 Flutter 应用中的后台处理,同时保持与现有插件生态系统的兼容性。

适合场景

  • 在后台运行重型计算
  • 处理大型数据集而不冻结用户界面
  • 在隔离环境中执行平台特定代码
  • 在密集任务期间提高应用程序的响应速度
  • 在并行工作流中与现有的 Flutter 插件集成

主要特点

  • 与 Flutter 插件无缝集成
  • 对 Android 和 iOS 平台的支持
  • 用于启动隔离区的简单 API
  • 主线程与隔离线程之间的安全通信
  • 用于隔离执行的内置错误处理

适合对象

  • 需要在Flutter中进行后台处理的开发者
  • CPU密集型操作的应用程序,如图像或音频处理
  • 使用需要隔离的 Flutter 插件的团队
  • 旨在提升UI性能的项目
  • 需要安全、隔离执行环境的应用程序

使用建议

  • 需要理解 Dart 的 isolate 和消息传递
  • 仅限 Android 和 iOS 平台
  • 由于隔离区开销,可能导致内存使用量增加
  • 不适用于 I/O 密集型任务,这些任务使用 async/await 即可
  • 跨隔离区通信在调试中可能存在复杂性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

FlutterIsolate

A Dart isolate is roughly equivalent to a single, independent execution thread. In a Flutter context, creating ("spawning") an isolate allows code to execute outside the main thread, which is important for running expensive/long-running tasks that would otherwise block the UI.

However, code in a spawned isolate will generally not be able to interact with Flutter plugins. This is due to the tight integration between the platform plugin scaffolding and the main application isolate.

The FlutterIsolate plugin fixes this with the introduction of a FlutterIsolate class, which is a wrapper around the platform APIs for creating isolates and setting up the bindings necessary for code running in spawned isolates to communicate with Flutter plugins.

FlutterIsolate API
Android iOS Description
FlutterIsolate.spawn(entryPoint,message) :white_check_mark: :white_check_mark: spawns a new FlutterIsolate
FlutterIsolate.pause() :white_check_mark: :white_check_mark: pauses a running isolate
FlutterIsolate.resume() :white_check_mark: :white_check_mark: resumed a paused isolate
FlutterIsolate.kill() :white_check_mark: :white_check_mark: kills a an isolate
FlutterIsolate.killAll() :white_check_mark: :white_check_mark: kills all currently running isolates
FlutterIsolate.runningIsolates :white_check_mark: :white_check_mark: returns the IDs associated with all currently running isolates
flutterCompute(callback,message) :white_check_mark: :white_check_mark: spawns a new FlutterIsolate, runs callback and returns the returned value
Usage

To spawn a FlutterIsolate, call the spawn method with a top-level or static function that has been annotated with the @pragma('vm:entry-point') decorator:

import 'package:flutter_isolate/flutter_isolate.dart';

@pragma('vm:entry-point')
void someFunction(String arg) { 
  print("Running in an isolate with argument : $arg");
}
...

class SomeWidget() {
...

@override
  Widget build(BuildContext context) {
    return ElevatedButton(
            child: Text('Run'),
            onPressed: () {
                FlutterIsolate.spawn(someFunction, "hello world");
            },
    );
}

If you just want to spawn an isolate to perform a single task (like the Flutter compute method), call flutterCompute:

@pragma('vm:entry-point')
Future<int> expensiveWork(int arg) async {
  int result;
  // lots of calculations
  return result;
}

Future<int> doExpensiveWorkInBackground() async {
  return await flutterCompute(expensiveWork, arg);
}

Isolates can also be spawned from other isolates:

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

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

@pragma('vm:entry-point')
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, decorated with the `@pragma('vm:entry-point') annotation:

@pragma('vm:entry-point')
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> {
  
  @pragma('vm:entry-point')
  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();
  }
}

Failure to add the @pragma('vm:entry-point') annotation will cause the app to crash in release mode.

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:

@pragma('vm:entry-point')
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);
  }
}

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目