You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用在iOS模拟器调用Firestore查询时崩溃求助

Flutter Firestore iOS模拟器崩溃问题解决

问题概述

Flutter应用以Firestore为数据库,Android端运行完全正常,但在iOS模拟器中调用Firestore更新操作时会立即崩溃。简化后的查询代码如下:

userCollection.doc("5m1K6U5FCrSqq2cBagywjpTWfDJ3").update({"age":60});

崩溃时Xcode核心日志:

Runner[8031:184013] -[NSNull intValue]: unrecognized selector sent to instance 0x7fff8a6d9fa0
2023-06-25 09:22:30.187535-0700 Runner[8031:184013] *** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', reason: '-[NSNull intValue]: unrecognized selector 
sent to instance 0x7fff8a6d9fa0'
...
libc++abi: **terminating with uncaught exception of type NSException**

使用的Firebase插件均为最新版本:

firebase_core: ^2.14.0
firebase_auth: ^4.4.0
cloud_firestore: ^4.8.1
firebase_storage: ^11.1.0

问题原因

Android端对Firestore设置中的null值兼容性更强,但iOS端的Firestore插件在解析Flutter层传递的null配置参数时,会将其转换为NSNull对象,随后尝试调用intValue方法(针对数值类型配置,如cacheSizeBytes),而NSNull并不具备该方法,直接触发崩溃。

解决方案

1. 检查Firestore初始化配置

排查Firestore初始化代码,确保所有数值类型的设置参数不传递null值:

  • 错误写法:
FirebaseFirestore.instance.settings = Settings(
  cacheSizeBytes: null, // 此处null会导致iOS端崩溃
);
  • 正确写法:
// 指定具体数值
FirebaseFirestore.instance.settings = const Settings(
  cacheSizeBytes: 10000000, // 10MB
);
// 或省略参数使用默认值
FirebaseFirestore.instance.settings = const Settings();

2. 清理iOS依赖与缓存

  • 执行Flutter清理命令:
flutter clean
  • 进入项目ios目录,执行Pod操作:
cd ios
pod deintegrate
pod install
  • 重启Xcode和iOS模拟器,通过模拟器菜单Device > Erase All Content and Settings清除模拟器缓存。

3. 验证Firebase配置文件

确保ios/Runner/GoogleService-Info.plist是从Firebase控制台最新下载的,无缺失或错误配置字段,尤其是Firestore相关项。

4. 适配iOS版本与SDK兼容性

  • 尝试升级iOS模拟器运行时版本(如从iOS 15.5升级到iOS 16+),部分旧iOS版本可能与最新Firebase插件存在兼容性问题。
  • 运行flutter doctor检查Flutter环境,确保iOS SDK版本与插件版本匹配,解决所有环境警告。

内容的提问来源于stack exchange,提问作者mohammad reza shabani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:23:25