((Flutter Web)Firebase Storage无法与Firebase插件一起使用

问题描述 投票:1回答:1

我最近为其中一个大量使用Firebase服务的项目添加了Flutter Web支持。除Firebase Storage无法正常工作外,其他一切似乎都正常运行。我知道firebase_storage插件当前不适用于Web,因此我尝试使用常规的[[firebase插件,但无法正常工作。

我有时会遇到不同的“红屏”错误,但是所有内容都与使用Firebase存储的页面有关。这是一个红色屏幕的示例:

enter image description here

这里是我先前成功使用

firebase_storage

和Android以及尝试使用firebase插件的代码的3个文件。import 'package:firebase/firebase.dart' as fb; // artiklar/images final fb.StorageReference fbRefArtiklarImages = fb.app().storage().ref().child("artiklar").child("images"); // guider/categoryImages final fb.StorageReference fbRefGuiderCategoryImages = fb.app().storage().ref().child("guider").child("categoryImages"); // guider/guideImages final fb.StorageReference fbRefGuiderGuideImages = fb.app().storage().ref().child("guider").child("guideImages"); // kalender/images final fb.StorageReference fbRefKalenderImages = fb.app().storage().ref().child("kalender").child("images"); // sidor/sidloggor final fb.StorageReference fbRefSidorSidloggorImages = fb.app().storage().ref().child("sidor").child("sidloggor"); // sidor/sidcovers final fb.StorageReference fbRefSidorSidcoversImages = fb.app().storage().ref().child("sidor").child("sidcovers"); // sidor/postImages/:sidaID/ final fb.StorageReference fbRefSidorPostImagesImages = fb.app().storage().ref().child("sidor").child("postImages"); // sidor/postImages/:sidaID/ final fb.StorageReference fbRefSidorKalenderImagesImages = fb.app().storage().ref().child("sidor").child("kalenderImages");
-

import 'dart:io'; import 'package:firebase/firebase.dart' as fb; class StorageService { //STORAGE REFERENCES final fb.Storage _storage = fb.app().storage("gs://astoria-site.appspot.com"); //UPLOADS IMAGE TO FIREBASE fb.UploadTask _uploadTask; Future<void> uploadStorageImage(File imageFile, String filePath) async { _uploadTask = _storage.ref().child(filePath).put(imageFile); return; } //DELETES IMAGE IN FIREBASE Future<void> deleteStorageImage(String filePath) async { try { await _storage.ref().child(filePath).delete(); } catch (e) { print(e.toString()); } return; } }

-

import 'package:astoria/theme/colors.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:firebase/firebase.dart'; import 'package:flutter/material.dart'; class FirebaseStorageImage extends StatelessWidget { final String fileName; final StorageReference storageLocation; FirebaseStorageImage({ @required this.fileName, @required this.storageLocation, }); Future<String> _getImageURL() async { final StorageReference ref = storageLocation.child(fileName + ".jpg"); try { var url = await ref.getDownloadURL(); return url.toString(); } catch (e) { return null; } } @override Widget build(BuildContext context) { return FutureBuilder( future: _getImageURL(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) { return Image( image: CachedNetworkImageProvider(snapshot.data), fit: BoxFit.cover, ); } else if (snapshot.connectionState == ConnectionState.waiting) { //RETURN THIS WHILE WAITING FOR IMAGE return Container(color: lightGreyColor); } else { //RETURN THIS IF NO IMAGE WAS FOUND AT THAT LOCATION return Image( image: AssetImage("assets/images/placeholder.png"), fit: BoxFit.cover, ); } }, ); } }

firebase flutter dart firebase-storage flutter-web
1个回答
1
投票
您需要将字符串传递给ref方法,它才能正常工作。为简便起见,将ref更改为refFromURL,然后按以下方式将存储段URL作为字符串传递给它;

fb.app().storage().refFromURL("YOUR BUCKET URL HERE eg: 'gs://project-ID.appspot.com'")

然后您可以安全地向其中添加其余方法,例如; child(),然后是put()putString()。祝你好运!
© www.soinside.com 2019 - 2024. All rights reserved.