首页 > 解决方案 > 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);

标签: firebaseflutterfirebase-storage

解决方案


您需要使用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


推荐阅读