firebase - Flutter 图像上传 - FIRStorageErrorDomain 的 Firebase 存储错误:根证书不受信任
问题描述
我正在尝试通过 Flutter 应用程序中的 iphone 模拟器将图像上传到 Firebase 存储。
我的上传代码是:
try {
FirebaseStorage _storage = FirebaseStorage.instance;
File image =
await ImagePicker.pickImage(source: ImageSource.gallery);
String filename = path.basename(image.path);
StorageReference reference = _storage.ref().child("images/");
StorageUploadTask uploadTask = reference.putFile(
image);
final StorageTaskSnapshot downloadUrl =
(await uploadTask.onComplete);
final String url = (await downloadUrl.ref.getDownloadURL());
print('URL Is $url');
setState(() {
_images.add(url);
});
} catch (e) {
print("Error received $e");
}
我正在使用版本
firebase_storage:^3.1.3
我收到以下错误
{
type = error;
value = "Root certificate is not trusted.";
} )
"LocalDataTask <0EE7042E-6F74-4086-BC11-B6953C86BB09>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <0EE7042E-6F74-4086-BC11-B6953C86BB09>.<1>, NSLocalizedDescription=cancelled}
我浏览了一下并注意到有一个看起来相似的旧错误已经解决,但是,因为我使用的是更新版本。我还看到了一篇关于此的旧 Stackoverflow 帖子,他们建议注销并重新登录......我也这样做了。我不确定这个错误意味着什么并且已经走到了死胡同。我能做些什么来解决这个问题?
我收到错误
StorageUploadTask uploadTask = reference.putFile(image);
解决方案
您需要使用HttpOverrides
绕过证书问题。
创建一个扩展类HttpOverrides
:
class _HttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext context) {
final HttpClient client = super.createHttpClient(context);
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
}
}
并调用 main.dart 中的类
void main() {
HttpOverrides.global = _HttpOverrides();
runApp(App());
}
}
参考:https ://api.flutter.dev/flutter/dart-io/HttpOverrides-class.html
推荐阅读
- iis - 使用 appcmd 设置 IIS10 网站的物理路径
- excel - excel,检查字符串是否包含字符 az 或 AZ
- java - Postgres 点类型列到 Spring Entity 数据类型
- node.js - 缺少节点方法?
- nginx - 如何设置nginx位置
- c# - 为什么我在运行管道时收到错误消息?
- android - 菜单中的 onOptionsItemSelected 对于使用 actionLayout 的项目是不可点击的
- java - 在线程 java.lang.RuntimeException 中获取异常:在同一阶段更改场景时 java.lang.reflect.InvocationTargetException
- java - 如何计算数组列表中的元素重复?
- c# - 有没有办法从导航页面更改主机窗口的大小和样式?(WPF)