Flutter集成iOS原生遇到的问题汇总:
下面列举一些最近在进行混编时遇到的一些问题:
Failed to register observatory port with mDNS with error -65555.
官方描述
On iOS 14 and higher, enable the Dart multicast DNS service in the Debug version of your app to add debugging functionalities such as hot-reload and DevTools via flutter attach.
解决方法
- 将应用程序的Info.plist重命名为Info-Debug.plist。复制一个名为Info-Release.plist的副本,并将其添加到Xcode项目中
- 在Info-Debug.plist中,添加键
Bonjour services
并将值设置为_dartobservatory._tcp
可选的设置Privacy - Local Network Usage Description
,并将值设置为Allow Flutter tools on your computer to connect and debug your application.This prompt will not appear on release builds.
- 在TARGETS中, build settings 修改 Info.plist File 路径 path/to/Info.plist 为
path/to/Info-$(CONFIGURATION).plist
.
In iOS 14+,debug mode Flutter apps can only be launched from Flutter tooling,IDEs with Flutter pl...
分析原因
在 iOS14 的真机上安装了 debug模式 编译出来的 flutter 应用,那么在断开编译安装连接后,将无法从桌面上打开该应用程序
解决方案
- 使用flutter的release模式,终端输入
flutter run --release
复制代码
- 修改main.dart的配置
Reason: tried: '/usr/lib/swift/App.framework/App' (no such file), '/usr/lib/swift/App.framework/App' (no such file),
分析原因
Flutter
的App.framework
没有导入
解决办法
打开 Pods-xx-frameworks.sh
,然后添加以下代码
install_framework "${PODS_ROOT}/../flutter/.ios/Flutter/App.framework"
但是这样只能一时解决问题,下次pod install
后又没有这个配置了,终极解决办法是:
重装Cocoapods和ruby-macho:
卸载ruby-macho和Cocoapods
sudo gem uninstall ruby-macho
sudo gem uninstall cocoapods
重新安装ruby-macho和Cocoapods
sudo gem install ruby-macho
sudo gem install cocoapods
复制代码
No podspec found for Flutter
in ../flutter/.ios/Flutter/engine
分析原因
这个是在这个路径下缺少这个文件
解决办法
可以看下其他Flutter项目下,是否有这个文件直接拷贝过来,这个就是和Flutter引擎相关的文件。假如有ios文件夹,看下这个文件下有没有那个文件
Undefined symbol: OBJC_CLASS$_TestViewController
分析原因
这个是我创建一个继承自FlutterViewController的类,没有发现
解决办法
在这个里面添加这个类的.m文件即可
底部Widget不跟随键盘弹起而弹起
分析原因
默认resizeToAvoidBottomInset:true,Scaffold 内部会将 mediaQuery.viewInsets.bottom 参与到 BoxConstraints 的大小计算,也就是键盘弹起时调整了内部的 bottom 位置来迎合键盘。
解决办法
设置 resizeToAvoidBottomInset: false
MissingPluginException(No implementation found for method resetPlugin on channel com.dooboolab.flutter_sound_player)
在9.2.13版本以前
,假如是iOS和Flutter混编项目,如果首次进入Flutter模块,这个插件使用是没有问题的,但是当离开这个模块第二次进入就会出现注册插件出错、初始化失败
的情况。报错如下:
ERROR during registerWithRegistrar: flutterSoundPlayerManager != nil
MissingPluginException(No implementation found for method resetPlugin on channel com.dooboolab.flutter_sound_player)
复制代码
分析原因
这是第二次进入,注册失败,其他插件都能正常注册,后来提了问题给开发人员
解决办法
这是插件确实存在的bug,目前作者已经在最新版本9.2.13
上做了修复。
后续再遇到新的问题,会继续更新的~
Could not build the precompiled application for the device. Error (Xcode): Undefined symbol: ___gxx_personality_v0
我单独在Xcode里面运行,发现是下面这个原因引起的,这个是在flutter里引入的录音插件 flutter_sound
分析原因
最开始让我百思不解,后来我才想到是不是这个插件里面用到了C++
解决办法
Xcode中 Build Phases->link Binary with Libraries中添加libc++.tbd
,这样就解决了这个问题
Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
解决办法
进行了项目混编,需要注册插件。
- iOS在AppDelegate中didFinishLaunchingWithOptions方法里面调用
GeneratedPluginRegistrant.register(with: self)
- Android添加 GeneratedPluginRegistrant.registerWith(flutterEngine)
Flutter配置抓包
需要手动配置IP
(_dio!.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.findProxy = (uri) {
return "PROXY localhost:8888";
};
//这个是Android手机抓包需要配置的,不然验证证书一直失败
client.badCertificateCallback =
(cert, host, port) => true;
};
复制代码
设置指示器的样式
SwiperCustomPagination(builder:
(BuildContext context, SwiperPluginConfig config) {
return Align(
alignment: const Alignment(0, 0.85),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children:
controller.imageList
.asMap()
.keys
.map((index) {
return index == config.activeIndex
? Container(
width: 16.0,
height: 6.0,
margin: const EdgeInsets.fromLTRB(
3.0, 0, 3.0, 0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(3)),
)
: Container(
width: 6.0,
height: 6.0,
margin: const EdgeInsets.fromLTRB(
3.0, 0, 3.0, 0),
decoration: BoxDecoration(
color: const Color(0x7FFFFFFF),
borderRadius: BorderRadius.circular(3)),
);
}).toList(),
),
);
})
复制代码
实现效果
No application found for TargetPlatform.ios. Is your project missing an ios/Runner/Info.plist? Consider running "flutter create ." to create one.
分析原因
项目代码中没有生成iOS工程文件
解决办法
在终端根据提示输入 flutter create .
即可自动生成iOS和Android工程代码。
ListView嵌套ListView,存在内容区域不显示
分析原因
主要是因为内容缺少了边界约束
解决办法
- 使用Container包裹着子ListView,并且设置高度;
- 修改ListView的shrinkWrap属性为True,因为True表示滚动方向的滚动视图内容是否应该由正在查看的内容所决定
集成百度地图插件时报错找不到头文件
解决办法
Xcode-TARGETS -> build settings -> Allow Non-modular Includes In Famework Modules设置为YES
复制代码
设置文本的最大宽度
ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 100,
),
child: const Text(
'设置文本的最大宽度',
overflow: TextOverflow.ellipsis,
softWrap: true,
style: TextStyle(
fontSize: 20,
),
) ,
)
复制代码
隐藏Flutter导航显示原生导航
让AppDelegate遵循UINavigationControllerDelegate
navigationController.delegate = self
实现代理方法
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//如果是Flutter页面,导航栏就隐藏
navigationController.navigationBar.isHidden = viewController.isKind(of: FlutterViewController.self)
}
复制代码
No application found for TargetPlatform.ios. Is your project missing an ios/Runner/Info.plist? Consider running "flutter create ." to create one.
在Android上面报这个错误 AndroidManifest.xml could not be found.
分析原因
这个是项目缺少ios和android目录
解决办法
当前项目下,运行 flutter create .
即可。
Failed assertion: line 269 pos 15: 'padding == null || padding.isNonNegative': is not true.
分析原因
设置的 padding 参数有问题
设置文本的最大宽度
ConstrainedBox(
constraints:BoxConstraints(
maxWidth: 100,
),
child:Text(
"设置文本的最大宽度",
overflow: TextOverflow.ellipsis,
softWrap:true,
style: TextStyle(
fontSize: 20,
),
),
),
复制代码
NetWork location failed because baidu location service check the key is unlegal, please check the key in AndroidManifest.xml !
分析原因
这个是集成百度地图后Android端定位出的问题,这个首先要检查,定位权限是否开启,GPS定位开关是否打开,如果都开启了,就要查看下百度开放平台的Android SHA1是否正确。
Xcode - TARGETS - build settings - Allow Non-modular Includes In Famework Modules 设置为 YES。
bottom overflowed by 10 PIXELS
分析原因
这就是当我们在一个固定大小的Widget中布局时,内容显示不全时报的问题
解决办法
使用 SingleChildScrollView
包裹要显示的内容,这样可以使得内容可以滚动,就解决了这个问题。但是SingleChildScrollView 不支持基于Sliver的延迟实例化模型。所以假如内容超过显示区域太多时,不建议使用这个。
异步网络请求完成后刷新UI
Future<String> fun1() async {
return '1';
}
Future<String> fun2() async {
return '2';
}
Future<String> fun3() async {
return '3';
}
void test() {
Future.wait([fun1(), fun2(), fun3()]).then((List responses) {
print(responses);
}).catchError((error) {
print(error);
});
}
复制代码
Flutter 版本的操作
- 查看当前Flutter版本:flutter --version
- 检查展示安装信息:flutter doctor
- 查看所有分支和当前分支:flutter channel
- 切换到指定分支:flutter channel stable
- 升级到最新版本:flutter upgrade
- 升级到指定版本:flutter upgrade v3.0.1
- 降级到指定版本:git reset --hard [commit_id]
- 这个commit_id是在这里找到的,首先打开 github.com/flutter/flu…
以上是最近我做新项目遇到的问题,但是这个里面没有讲关于项目的架构设计、功能封装、插件选型这些。这里只是记录在开发中遇到的问题,后续会把项目遇到的问题持续更新!!!