英文:
getting the x number of multiple images being uploaded in firebase flutter
问题
我目前有一个用于存储本地图像、将其上传到Firestore存储并将其写入Cloud Firestore的工作代码。现在一切都正常工作。但是,我正在处理指示器的用户界面。
我希望用户能看到他们上传了多少张图片,例如"已上传10张中的1张"。我看不到类似的逻辑,或者可能我不知道如何做,任何想法都会非常感激。
未来 uploadImage() async {
String catchUrls;
// 循环,重复遍历存储在 List<XFile> imageFileList 中的文件
for (var image in imageFileList!) {
// 创建新的文件名
String uniqueFileName = DateTime.now().microsecondsSinceEpoch.toString();
// 获取当前文件名
String currentFileName = image.path;
// 用新文件名替换
String newFileName =
currentFileName.replaceAll(currentFileName, uniqueFileName);
// 引用文件的位置。
Reference reference =
FirebaseStorage.instance.ref().child("images").child(newFileName);
// 上传文件
await reference.putFile(File(image.path));
// 获取下载链接
catchUrls = await reference.getDownloadURL();
// 将下载链接存储到 List<String> getDownloadUrls 中。
getDownloadUrls.add(catchUrls);
print(getDownloadUrls);
}
}
英文:
I currently have a working code for storing the local images, uploading it in firestore storage and writing it on cloud firestore. It's all working now. However I am working with the UI for indicators.
I want the user to see how many of their images have been uploaded for example "uploaded 1 out of 10 of images". I can't see a similar logic or maybe i do not have any clue on how to do it, any ideas are greatly appreciated.
Future uploadImage() async {
String catchUrls;
// loop, reiterate the files stored in List<XFile> imageFileList
for (var image in imageFileList!) {
// create new file name
String uniqueFileName = DateTime.now().microsecondsSinceEpoch.toString();
// get current file name
String currentFileName = image.path;
// replace with new file na,e
String newFileName =
currentFileName.replaceAll(currentFileName, uniqueFileName);
// reference the location of file.
Reference reference =
FirebaseStorage.instance.ref().child("images").child(newFileName);
// upload file
await reference.putFile(File(image.path));
// get the download urls
catchUrls = await reference.getDownloadURL();
// store the download urls to the List<String>getDownloadUrls.
getDownloadUrls.add(catchUrls);
print(getDownloadUrls);
}
}
答案1
得分: 1
你可以像以下这样使用 StreamController
:
StreamController<int> streamController = StreamController();
Future uploadImage() async {
String catchUrls;
// 循环,重复处理存储在 List<XFile> imageFileList 中的文件
for (var image in imageFileList!) {
// 创建新文件名
String uniqueFileName = DateTime.now().microsecondsSinceEpoch.toString();
// 获取当前文件名
String currentFileName = image.path;
// 替换为新文件名
String newFileName =
currentFileName.replaceAll(currentFileName, uniqueFileName);
// 引用文件的位置
Reference reference =
FirebaseStorage.instance.ref().child("images").child(newFileName);
// 上传文件
await reference.putFile(File(image.path));
// 获取下载链接
catchUrls = await reference.getDownloadURL();
// 将下载链接存储到 List<String> getDownloadUrls 中
getDownloadUrls.add(catchUrls);
streamController.add(getDownloadUrls.length);
print(getDownloadUrls);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Hello'),
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () async {
await uploadImage();
},
child: const Text('Upload Images'),
),
StreamBuilder<int>(
stream: streamController.stream,
initialData: 0,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
'uploaded ${snapshot.data} out of ${imagesFileList.length} of images');
}
return Text('Waiting...');
},
),
],
),
),
);
}
在每次将图像添加到 FirebaseStorage 时,您都将 getDownloadedUrl
的长度添加到流中。在 StreamBuilder
中,您获取了该长度。
英文:
You can use StreamController
like following:
StreamController<int> streamController = StreamController();
Future uploadImage() async {
String catchUrls;
// loop, reiterate the files stored in List<XFile> imageFileList
for (var image in imageFileList!) {
// create new file name
String uniqueFileName = DateTime.now().microsecondsSinceEpoch.toString();
// get current file name
String currentFileName = image.path;
// replace with new file na,e
String newFileName =
currentFileName.replaceAll(currentFileName, uniqueFileName);
// reference the location of file.
Reference reference =
FirebaseStorage.instance.ref().child("images").child(newFileName);
// upload file
await reference.putFile(File(image.path));
// get the download urls
catchUrls = await reference.getDownloadURL();
// store the download urls to the List<String>getDownloadUrls.
getDownloadUrls.add(catchUrls);
streamController.add(getDownloadUrl.length);
print(getDownloadUrls);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Hello'),
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () async {
await uploadImage();
},
child: const Text('Upload Images'),
),
StreamBuilder<int>(
stream: streamController.stream,
initialData: 0,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
'uploaded ${snapshot.data} out of ${imagesFileList.length} of images');
}
return Text('Waiting...');
},
),
],
),
),
);
}
Here at every iteration of adding image to FirebaseStorage you are adding length of getDownloadedUrl
to the stream. And in StreamBuilder
you are getting that length.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论