简体   繁体   English

Flutter&Firebase-与firebase_auth一起使用firebase_storage

[英]Flutter & Firebase - using firebase_storage along with firebase_auth

Working with the firebase packages: https://pub.dartlang.org/packages/firebase_auth https://pub.dartlang.org/packages/firebase_storage 使用Firebase软件包: https : //pub.dartlang.org/packages/firebase_auth https://pub.dartlang.org/packages/firebase_storage

I can sign into firebase using firebase_auth, and everything works great there, but when I use the firebase_storage package to upload a file I get access denied and I have to go into firebase and set the permissions to: 我可以使用firebase_auth登录到firebase,并且在那里一切正常,但是当我使用firebase_storage包上传文件时,访问被拒绝,我必须进入firebase并将权限设置为:

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write;//: if request.auth != null;
   }
 }
}

This is not ideal for an application that uses authentication, how do I tell the firebase_storage package that I am authenticated via the firebase_auth package? 对于使用身份验证的应用程序来说,这不是理想的选择,我如何告诉firebase_storage软件包已通过firebase_auth软件包进行了身份验证?

You should not have that problem, I share a code that works for me. 您应该没有那个问题,我共享一个对我有用的代码。

Future<Null> ensureLoggedIn() async {
  FirebaseUser firebaseUser = await auth.currentUser();
  assert(firebaseUser != null);
  assert(!firebaseUser.isAnonymous);
}

Future<String> uploadFile(File file) async {
    assert(file != null);
    await ensureLoggedIn();
    int random = new Random().nextInt(100000);
    String _instance = '/files/image_$random.jpg';

    StorageReference ref = FirebaseStorage.instance.ref().child(_instance);
    StorageUploadTask uploadTask = ref.putFile(file);

    final Uri downloadUrl = (await uploadTask.future).downloadUrl;

    return downloadUrl.toString();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter - firebase_auth updateProfile 方法不起作用 - Flutter - firebase_auth updateProfile method is not working 如何使用 Firebase_Auth 和 Flutter 登录 Twitter - How to Sign In with Twitter using Firebase_Auth with Flutter Flutter firebase_auth signInWithCredential 不适用于 android - Flutter firebase_auth signInWithCredential is not working on android Dart Flutter 顺序 firebase_auth 错误 - Dart Flutter sequential firebase_auth errors Flutter 和 Firebase:任务 &#39;:firebase_auth:compileDebugJavaWithJavac&#39; 执行失败 - Flutter and Firebase: Execution failed for task ':firebase_auth:compileDebugJavaWithJavac' Firebase_auth 无法在 flutter [firebase_auth: ^0.16.1] 上构建发布 apk - Firebase_auth cannot build release apk on flutter [firebase_auth: ^0.16.1] 将firebase_storage包含在flutter应用程序中的最佳方法是什么? - What is the best way to include firebase_storage in a flutter app? Flutter Advance_pdf_viewer 和 firebase_storage 的依赖问题 - Flutter Dependency problem with advance_pdf_viewer and firebase_storage 未处理的异常:[firebase_auth/unknown] null:使用颤振/firebase 进行电话身份验证时出错 - Unhandled Exception: [firebase_auth/unknown] null: Error in Phone Authentication using flutter/firebase Flutter - 尝试捕捉不处理 firebase_auth 错误 - Flutter - try and catch dont't handle firebase_auth errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM