【问题标题】:Flutter image upload - Firebase storage errors with FIRStorageErrorDomain : Root certificate is not trustedFlutter 图像上传 - FIRStorageErrorDomain 的 Firebase 存储错误:根证书不受信任
【发布时间】:2020-06-15 09:14:25
【问题描述】:

我正在尝试通过 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( 图片);

【问题讨论】:

    标签: firebase flutter firebase-storage


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用此方法上传图片到存储并获取下载地址:-

      Future<String> _uploadFileToFirebase(String path, File file) async {
         StorageReference reference = FirebaseStorage.instance.ref().child(path);
         StorageUploadTask task = reference.putFile(file);
         return await (await task.onComplete).ref.getDownloadURL();
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-17
        • 1970-01-01
        • 2015-02-14
        • 2015-01-24
        • 2021-12-10
        • 2019-02-26
        • 2011-07-05
        • 2021-05-31
        • 1970-01-01
        相关资源
        最近更新 更多