文章目录
-
-
-
-
- 创建一个flutter plugin项目
-
- 引入依赖
-
- 创建pigeons文件夹和message.dart
-
- 执行生成各个平台文件的命令
-
- base_plugin.dart
-
- BasePlugin.kt
-
- BasePlugin.swift
-
- 遇到的问题
-
-
-
1. 创建一个flutter plugin项目
2. 引入依赖
pigeon: ^18.0.0
3. 创建pigeons文件夹和message.dart
dart
import 'package:pigeon/pigeon.dart';
// 输出配置
// 控制台执行:dart run pigeon --input pigeons/message.dart
@ConfigurePigeon(PigeonOptions(
dartOut: './lib/messages.g.dart',
dartPackageName: 'base_plugin',
kotlinOut: 'android/src/main/kotlin/com/app/base_plugin/Messages.g.kt',
kotlinOptions: KotlinOptions(
// copyrightHeader: ['zero'],
package: 'com.app.base_plugin',
),
swiftOut: 'ios/Classes/Messages.g.swift',
swiftOptions: SwiftOptions(),
))
@HostApi()
abstract class HostMessageApi {
String flutter2Native(String message, int type);
@async
String flutter2NativeAsync(String message, int type);
}
@FlutterApi()
abstract class FlutterMessageApi {
String native2Flutter(String message, int type);
@async
String native2FlutterAsync(String message, int type);
}
4. 执行生成各个平台文件的命令
dart run pigeon --input pigeons/message.dart
5. base_plugin.dart
dart
import 'messages.g.dart';
class BasePlugin {
static final HostMessageApi _api = HostMessageApi();
static Future<String> flutter2Native(String message, int type) async {
return await _api.flutter2Native(message, type);
}
static Future<String> flutter2NativeAsync(String message, int type) async {
return await _api.flutter2NativeAsync(message, type);
}
}
6. BasePlugin.kt
kt
package com.app.base_plugin
import androidx.annotation.NonNull
import com.app.base_plugin.FlutterMessageApi
import com.app.base_plugin.HostMessageApi
import io.flutter.embedding.engine.plugins.FlutterPlugin
/** BasePlugin */
class BasePlugin: FlutterPlugin, HostMessageApi {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var flutterMessageApi: FlutterMessageApi
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
print("onAttachedToEngine")
HostMessageApi.setUp(flutterPluginBinding.binaryMessenger, this)
flutterMessageApi = FlutterMessageApi(flutterPluginBinding.binaryMessenger)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
HostMessageApi.setUp(binding.binaryMessenger, null)
}
override fun flutter2Native(message: String, type: Long): String {
print("flutter2Native=message=$message=type=$type")
flutterMessageApi.native2Flutter("111", callback = {
print("1111")
}, typeArg = 1)
return message
}
override fun flutter2NativeAsync(
message: String,
type: Long,
callback: (Result<String>) -> Unit
) {
fun d(e:Result<String>){
print("d")
}
print("flutter2NativeAsync=message=$message=type=$type")
flutterMessageApi.native2FlutterAsync("2222", callback = {
print("222")
}, typeArg = 2)
callback(Result.success("flutter2NativeAsync=message=$message=type=$type"));
}
}
7. BasePlugin.swift
swift
import Flutter
import UIKit
public class BasePlugin: NSObject, FlutterPlugin,HostMessageApi {
private static var flutterAPI: FlutterMessageApi? = nil
public static func register(with registrar: any FlutterPluginRegistrar) {
let api = BasePlugin()
HostMessageApiSetup.setUp(binaryMessenger: registrar.messenger(), api: api)
flutterAPI = FlutterMessageApi(binaryMessenger: registrar.messenger())
}
func flutter2Native(message: String, type: Int64) throws -> String {
print("ios->flutter2NativeMessage===="+message);
return "ios->flutter2Native=message=\(message)=type=\(type)"
}
func flutter2NativeAsync(message: String, type: Int64, completion: @escaping (Result<String, any Error>) -> Void) {
print("ios->flutter2NativeAsyncMessage===2")
completion(.success(message))
}
}
8. 遇到的问题
Parse Issue (Xcode): Module 'base_plugin' not found
- 删除ios文件下面的podfile和podfile.lock
- Messages.g.swift 和BasePlugin.swift 要放到Classes下面
Pigeon: error: type 'FlutterError' does not conform to protocol 'Error'
- 在Messages.g.swift中添加extension FlutterError: Swift.Error {}