Flutter升级后调用Mongo Realm函数出现证书过期等错误求解决
问题描述
开发Flutter应用,使用Mongo Realm实现认证、索引搜索及函数调用功能,原本运行正常,但执行flutter upgrade后启动应用抛出以下错误:
D/libGLESv2( 6862): STS_GLApi : DTS, ODTC are not allowed for Package : com.example.auction_app I/flutter ( 6862): 2023-11-18T22:15:45.054885: [ERROR] Realm: HTTP Transport: Exception executing get https://us-east-1.aws.realm.mongodb.com/api/client/v2.0/app/application-0-ont/location: HandshakeException: Handshake error in client (OS Error: I/flutter ( 6862): CERTIFICATE_VERIFY_FAILED: certificate has expired(handshake.cc:393)) I/flutter ( 6862): an error has occured during retrieveAllProducts function I/flutter ( 6862): AppException: non-zero custom status code considered fatal, status code: 999 I/flutter ( 6862): [] I/flutter ( 6862): 2023-11-18T22:15:45.240942: [ERROR] Realm: App: log_in_with_credentials failed: 0 message: non-zero custom status code considered fatal
已尝试在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" />但无效,Realm应用已允许全网访问并开启匿名认证,核心业务代码如下:
Future<List<dynamic>> retrieveAllProducts(arg) async { print("RetrieveAllProducts Function"); try { final user = await app.logIn(emailPwCredentials); products = await user.functions.call('allProducts'); } catch (e) { print('an error has occured during retrieveAllProducts function'); print(e); } print(products); return products; }
解决方案
更新Realm Flutter SDK版本
Flutter升级后,旧版本Realm SDK可能存在兼容性问题。打开pubspec.yaml,将realm依赖更新到最新兼容版本,执行flutter pub get重新安装依赖。排查证书验证异常
核心错误为证书过期验证失败,可按以下步骤处理:- 检查设备系统时间是否正确、时区是否匹配,错误的系统时间会导致证书有效期判断异常;
- 针对Android配置网络安全规则(仅用于测试,生产环境禁用):
- 在
android/app/src/main/res/xml/下创建network_security_config.xml,内容如下:<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">us-east-1.aws.realm.mongodb.com</domain> <trust-anchors> <certificates src="system" /> <certificates src="user" /> </trust-anchors> </domain-config> </network-security-config> - 在
AndroidManifest.xml的application标签中添加android:networkSecurityConfig="@xml/network_security_config"。
- 在
清理缓存与构建文件
执行以下命令清理旧缓存和构建文件,避免版本冲突:flutter clean flutter pub get cd android && ./gradlew clean cd .. flutter run验证Realm端证书状态
通过浏览器访问https://us-east-1.aws.realm.mongodb.com查看证书有效期,若证书确实过期,需在MongoDB控制台更新对应域名的证书。
内容的提问来源于stack exchange,提问作者Djamel Abbou
相关产品推荐
相关产品推荐

