dart 获取Firebase Flutter中正在上传的多个图像的x数量

s4n0splo  于 2023-07-31  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我目前有一个工作代码存储本Map像,上传到firestore存储和写在云firestore。现在一切正常然而,我正在使用UI的指标。我想让用户看到有多少他们的图像已被上传,例如“上传了10个图像1”。我看不出类似的逻辑,或者也许我没有任何关于如何做到这一点的线索,任何想法都非常赞赏。

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);
    }
  }

字符串

brvekthn

brvekthn1#

您可以像下面这样使用StreamController

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...');
              },
            ),
          ],
        ),
      ),
    );
  }

字符串
在这里,在每次向FirebaseStorage添加图像的迭代中,您都要向流添加长度为getDownloadedUrl的图像。在StreamBuilder中,你得到了这个长度。

相关问题