简体   繁体   English

Flutter Firebase 存储数据不立即显示数据

[英]Flutter Firebase Storage Data doesn't show Data immediately

I'm currently working on a project in flutter where I want to store uploaded Data in Firebase Storage.我目前正在 flutter 中开展一个项目,我想将上传的数据存储在 Firebase 存储中。 This works fine but now I'm facing a problem with showing the data.这工作正常,但现在我在显示数据时遇到了问题。 I have to do a restart for showing the uploaded Data in my List.我必须重新启动才能在我的列表中显示上传的数据。

I hope someone can help me with this issue.我希望有人可以帮助我解决这个问题。

onPressed: () async {
  FilePickerResult? result = await FilePicker.platform
      .pickFiles(allowMultiple: true);
  if (result == null) return;

  final path = result.files.single.path!;

  setState(() {
    
  });
  final fileName = result.files.single.name;

  storage
      .uploadFile(path, fileName)
      .then((value) => print('Done'));
},

This is my call function when pressing the button.这是我按下按钮时拨打的电话 function。

Future<void> uploadFile(String destination, String fileName) async {
    final User? user = auth.currentUser;
    final uid = user!.uid;
    File file = File(destination);
    try {
      await storage.ref('$uid/$fileName').putFile(file);
    } on firebase_core.FirebaseException catch (e) {
      print(e);
    }
  }

This is my method for pushing the data into firebase storage.这是我将数据推送到 firebase 存储的方法。

Container(
  width: MediaQuery.of(context).size.width - 15,
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(25),
  ),
  child: FutureBuilder(
    future: _loadImages(),
    builder: (context,
        AsyncSnapshot<List<Map<String, dynamic>>> snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        return Row(
          children: [
            Expanded(
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: snapshot.data?.length ?? 0,
                itemBuilder: (context, index) {
                  final Map<String, dynamic> image =
                      snapshot.data![index];

                  return Card(
                    shape: RoundedRectangleBorder(
                        borderRadius:
                            BorderRadius.circular(20)),
                    elevation: 0,
                    child: ListTile(
                      dense: false,
                      contentPadding: EdgeInsets.all(15),
                      leading: Image.network(
                        image['url'],
                      ),
                      trailing: IconButton(
                        onPressed: () => _delete(image['path']),
                        icon: const Icon(
                          Icons.delete,
                          color: Colors.red,
                        ),
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        );
      }

      return const Center(
        child: CircularProgressIndicator(),
      );
    },
  ),
),

And this is how I display my files in my app.这就是我在应用程序中显示文件的方式。

Looking forward for some help.期待一些帮助。

I want to see my data directly when uploading it into Firebase Storage.我想在将数据上传到 Firebase 存储时直接看到我的数据。

If you want to show the newly uploaded image right away, you will have to force a refresh of the code that renders the list of images.如果您想立即显示新上传的图像,则必须强制刷新呈现图像列表的代码。 If you're using a StatefulWidget to render the images, you could for example call setState() on that widget after the upload has completed.如果您使用StatefulWidget来呈现图像,您可以在上传完成后调用该小部件上的setState() If you're using another state management approach, you'd do the equivalent for that approach.如果您使用的是另一种 state 管理方法,您会为该方法执行等效操作。

But note that this will only work on the device of the user who uploaded the image.但请注意,这仅适用于上传图片的用户的设备。 Any other devices will still only see the new image if they manually restart the rendering of the list of images.如果任何其他设备手动重新启动图像列表的渲染,它们仍然只能看到新图像。

A common way to work around this is to store the list of image URLs in one of Firebase's databases (Realtime Database or Cloud Firestore), and render it from there with a StreamBuilder .解决此问题的一种常见方法是将图像 URL 列表存储在 Firebase 的一个数据库(实时数据库或 Cloud Firestore)中,然后使用StreamBuilder从那里渲染它。 The StreamBuilder continues to listen for updates to the database, so if one user's image upload completes and they write the URL/path of the image to the database, that immediately refreshes the list of images not just on their device, but on all other connected devices too. StreamBuilder继续监听数据库的更新,因此如果一个用户的图像上传完成并且他们将图像的 URL/路径写入数据库,这将立即刷新图像列表,不仅在他们的设备上,而且在所有其他连接的设备上设备太。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM