颤振Firebase-与firebase_auth一起使用firebase_storage [英] Flutter & Firebase - using firebase_storage along with firebase_auth
问题描述
使用firebase软件包: https://pub.dartlang.org/packages/firebase_auth https://pub.dartlang.org/packages/firebase_storage
Working with the firebase packages: https://pub.dartlang.org/packages/firebase_auth https://pub.dartlang.org/packages/firebase_storage
我可以使用firebase_auth登录到firebase,并且在那里一切正常,但是当我使用firebase_storage软件包上传文件时,访问被拒绝,我必须进入firebase并将权限设置为:
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:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;//: if request.auth != null;
}
}
}
这对于使用身份验证的应用程序来说不是理想的选择,我如何告诉firebase_storage软件包已通过firebase_auth软件包进行了身份验证?
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?
推荐答案
您应该没有这个问题,我分享了一个对我有用的代码.
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();
}
这篇关于颤振Firebase-与firebase_auth一起使用firebase_storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!