如何在 flutter 中流式传输 firebase 存储中的随机视频?

How to stream random videos in firebase storage with flutter?

我有一个有点像 youtube 的应用程序,我想在其中构建随机视频 links,我知道如何从 firebase 获取带有 link 的视频,但我希望它随机 links,有什么想法吗?

上传到 Firebase 存储时

string name = DateTime.now().millisecondsSinceEpoch.toString();
//sample name 1351441456747
     final StorageReference storageReference =
                      FirebaseStorage().ref().child("posts/$name");
    
                  final StorageUploadTask uploadTask =
                      storageReference.putFile(videofile);
    
                  final StreamSubscription<StorageTaskEvent> streamSubscription =
                      uploadTask.events.listen((event) {
                    // You can use this to notify yourself or your user in any kind of way.
                    // For example: you could use the uploadTask.events stream in a StreamBuilder instead
                    // to show your user what the current status is. In that case, you would not need to cancel any
                    // subscription as StreamBuilder handles this automatically.
                   
                   
                    // Here, every StorageTaskEvent concerning the upload is printed to the logs.
                    print('EVENT ${event.type}');
                  });
    
    // Cancel your subscription when done.
                  await uploadTask.onComplete;
                  streamSubscription.cancel();
                  String videourl =
                      await (await uploadTask.onComplete).ref.getDownloadURL();

然后将对象上传到 firestore 或实时数据库

  final DocumentReference documentReference =
                  await Firestore.instance.collection("posts").add({
                "postId": "postId",
              });
              String postId = documentReference.documentID;
              Firestore.instance.collection("posts").document(postId).setData({
                "postId": postId,
                "userId": userId,                    
                "videourl": videourl,
                "timestamp" :name,
                
              });

现在您在数据库中有了您的对象。在阅读期间,您必须在此处对数据进行排序。

示例如何显示随机视频

获取所有文档并将它们放入列表中并生成一个随机数并获取该索引处的对象

阅读更多关于 firestore operations here