Flutter 多线程调用

本文详细介绍了Flutter中如何使用Isolate进行异步处理,通过创建ReceivePort和SendPort实现数据通信,避免阻塞UI线程。示例代码展示了如何在Isolate中执行耗时任务并返回结果,以及在完成任务后正确释放资源的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 void test() async {
    print('外部代码1');
//  创建Port 端口
    ReceivePort receivePort = ReceivePort();
//  创建Isolate
    Isolate iso = await Isolate.spawn(oneWayEntryPoint, receivePort.sendPort);

    receivePort.listen((message) {
      print('端口回来的数据a是$message');
      receivePort.close();
      iso.kill(priority: Isolate.immediate); //使用完毕之后释放
      iso = null;
      print('2333');
    });
  }

  static oneWayEntryPoint(SendPort sendPort) async {

    // 处理耗时任务
    // ........
    sendPort.send(1000);
  }
要在 Flutter调用 Android 原生代码,可以通过使用 Flutter 的平台通道(Platform Channel)来进行交互。下面是一个简单的示例,演示了如何调用 Android 原生代码: 1. 在 Flutter 项目的 `lib` 目录下创建一个新的 Dart 文件(例如 `native.dart`),用于定义与原生代码交互的接口。 ```dart import 'package:flutter/services.dart'; class NativeCode { static const platform = MethodChannel('com.example.app/native'); static Future<void> callNativeMethod() async { try { await platform.invokeMethod('nativeMethod'); } catch (e) { print(e); } } } ``` 2. 在 Android 项目的 `android/app/src/main/java/com/example/app` 目录下创建一个新的 Java 类(例如 `NativeChannel.java`),用于处理 Flutter 与原生代码之间的通信。 ```java package com.example.app; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; public class NativeChannel implements FlutterPlugin, MethodCallHandler { private MethodChannel channel; @Override public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) { channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "com.example.app/native"); channel.setMethodCallHandler(this); } @Override public void onMethodCall(MethodCall call, Result result) { if (call.method.equals("nativeMethod")) { // 在这里编写你的原生代码逻辑 // ... result.success(null); // 返回结果给 Flutter } else { result.notImplemented(); } } @Override public void onDetachedFromEngine(FlutterPluginBinding binding) { channel.setMethodCallHandler(null); channel = null; } } ``` 3. 在 `MainActivity.java` 中注册 NativeChannel 插件: ```java import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.engine.FlutterEngine; public class MainActivity extends FlutterActivity { @Override public void configureFlutterEngine(FlutterEngine flutterEngine) { super.configureFlutterEngine(flutterEngine); flutterEngine.getPlugins().add(new NativeChannel()); } } ``` 4. 在 Flutter调用原生方法,例如在一个按钮的点击事件中: ```dart ElevatedButton( onPressed: () { NativeCode.callNativeMethod(); }, child: Text('调用原生方法'), ) ``` 通过以上步骤,你就可以在 Flutter调用 Android 原生代码了。你可以根据需要扩展这个示例,添加更多的方法和参数来实现更复杂的交互。记得在调用原生方法时,遵循正确的线程调度和错误处理方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值