flutter插件开发
# pigeon使用
# 开发流程
开发具体流程可见:
https://juejin.cn/post/7006554771674906637
以plugin的模式创建,以自动初始化
flutter create --template=plugin --platforms=android,ios -i swift -a java --org com.hss01248 location_util
1
拷贝根目录下pigeon/schema.dart文件夹和run_pigeon.sh文件到根目录
修改pigeon/schema.dart方法定义
修改run_pigeon.sh里目标路径.
dev_dependencies:
pigeon: ^1.0.0
1
2
2
运行run_pigeon.sh.无日志则生成代码成功.
# 写Android和ios实现
# 注册pigeon
public class LocationUtilPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "location_util");
channel.setMethodCallHandler(this);
//上面是模板生成的,下面是自己手动注册:
LocationPigeon.LocationUtil.setup(flutterPluginBinding.getFlutterEngine().getDartExecutor().getBinaryMessenger(), new LocationImpl());
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 发布:
参考官方文档: https://doc.flutterchina.club/developing-packages/
- 配置好路径
name: location_util
description: flutter plugin for LocationUtil.
version: 0.0.1
homepage:
publish_to: http://pub.xxx.com/
1
2
3
4
5
2
3
4
5
配置作者: https://www.cnblogs.com/mengqd/p/13928830.html
测试发布
flutter packages pub publish --dry-run
1发布
flutter packages pub publish
1
# 引用
dependencies:
location_util:
version: ^0.0.1
hosted:
name: location_util
url: https://xxx.xx.ccc/
1
2
3
4
5
6
2
3
4
5
6
# 入参
类型安全 or json/map?
# 返回值
参考后台接口返回: data,code,msg形式, 不抛出exception,而是以code,msg形式返回.
msg在debug时返回整个堆栈,在release时返回友好的翻译信息
# exception处理
需要await才能catch住
try{
await _flipperNetworkPlugin.reportRequest(uniqueId,DateTime.now().millisecondsSinceEpoch,
'${options.baseUrl}${options.path}',options.method,headers,body);
}catch(e,s){
debugPrint("print by dio-flutter: reportRequest--> "+e.toString());
//+s.toString()
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 兼容性问题处理
https://juejin.cn/post/7135737121863630879
->条件导入/导出
import 'platform.dart' if (dart.library.html) 'web.dart';
import 'dart:io' if (dart.library.html) 'dart:html';
1
2
2
->分包
->联邦插件
编辑 (opens new window)
上次更新: 2022/09/30, 21:58:18