![](/img/trans.png)
[英]Flutter firebase_storage FirebaseStorage.instance(): The expression doesn't evaluate to a function, so it can't be invoked
[英]My FirebaseStorage method isn't Running in Flutter. After the `firebase_storage: ^4.0.0`
任何人都可以提供帮助我正在尝试将我的图像发送到 firebase 存储,然后检索下载 URL 并保存到 firebasefirestore。 但是我的 FirebaseStorage 方法没有运行。
firebase_storage 之后发生的事情firebase_storage: ^4.0.0
更新
我的
FirebaseStorage().ref().child('path).putFile()
方法没有运行
FirebaseStorage()
.ref()
.child("Accounts Posts/Images/").putFile(//MyFIle);
static final FirebaseStorage storage = FirebaseStorage.instance;
static final FirebaseFirestore _firestore = FirebaseFirestore.instance;
Future<dynamic> sendData(SearchTileModel st, List<File> data) async {
bool completed = false;
CollectionReference _collec = _firestore.collection('Accounts Posts');
List<String> _imageUrls = [];
print(data.length);
data.length > 0
? data.asMap().forEach((
index,
element,
) async {
print(index);
print(element);
String downloadUrl;
/////// HERE this code doesn't get called up ////////////
StorageTaskSnapshot snapshot = await FirebaseStorage()
.ref()
.child("Accounts Posts/Images/${index}")
.putFile(element)
.onComplete
.then((value) {
downloadUrl = value.ref.getDownloadURL().toString();
return;
});
print(downloadUrl);
print(snapshot);
print(snapshot.error);
if (snapshot.error == null) {
await snapshot.ref
.getDownloadURL()
.then((value) => downloadUrl = value.toString());
print(downloadUrl);
_imageUrls.insert(index, downloadUrl);
if (st.images.length == _imageUrls.length) {
SearchTileModel newModel = st;
newModel.images = _imageUrls;
await _collec
.doc('${newModel.placeName} : name')
.set(
st.toJson(),
SetOptions(mergeFields: [
'images',
]),
)
.whenComplete(() {
return completed = true;
}).catchError((onError) {
print('${onError.toString()}');
completed = false;
});
}
} else {
completed = false;
print('Error from image repo ${snapshot.error.toString()}');
throw ('This file is not an image');
}
})
: print('No Images Selected');
if (data.length == 0) {
SearchTileModel newModel = st;
newModel.images = _imageUrls;
await _collec
.doc('${newModel.placeName} : name')
.set(
st.toJson(),
SetOptions(mergeFields: [
'images',
]),
)
.whenComplete(() {
return completed = true;
}).catchError((onError) {
print('${onError.toString()}');
completed = false;
});
}
return completed;
}
所有显示该方法的打印语句都没有运行。 我还认为由于await
需要时间,但等待 10 分钟后没有发生任何事情。
I/flutter (14005): true
I/flutter (14005): redtfgn
I/flutter (14005): File:
'/storage/emulated/0/Android/data/com.example.medium/files/Pictures/scaled_ec614e8a-0522-4cae-97be-
3ed50356de9c3343447263121135351.jpg'
I/flutter (14005): 1
I/flutter (14005): running
I/flutter (14005): 0
I/flutter (14005): File:
'/storage/emulated/0/Android/data/com.example.medium/files/Pictures/scaled_ec614e8a-0522-4cae-97be-
3ed50356de9c3343447263121135351.jpg'
I/flutter (14005): Till Here the code is Running
更新得到这些行
I/BiChannelGoogleApi(17801): [FirebaseAuth: ] getGoogleApiForMethod()
returned Gms: com.google.firebase.auth.api.internal.zzaq@cdf79f2
E/StorageUtil(17801): error getting token
java.util.concurrent.TimeoutException: Timed out waiting for Task
D/NetworkSecurityConfig(17801): No Network Security Config specified, using
platform default
W/NetworkRequest(17801): no auth token for request
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write ;
}
}
}
我可以看到您正在使用.ref()来获取参考。 查看文档,可以看到使用的是getReference()
StorageReference storageRef = storage.getReference();
因此,最好按照文档中的说明设置存储引用。 此外,上传文件的代码如下:
Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
uploadTask = riversRef.putFile(file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.