简体   繁体   中英

How can I check if a Documentsnapshot has data?

Im trying to figuring out if videos exists inside a collection or not and if they exist I wanna show them and showing them works actually but if not then I wanna print a default text like No videos yet but I dont know how I can do this. Maybe anyone can help. Heres my code in the Inkwell I displaying all videos

FutureBuilder(
                        future: myVideos,
                        builder: (BuildContext context, snapshot) {
                          if (!snapshot.hasData) {
                            return Container();
                          }
                          return StaggeredGridView.countBuilder(
                            scrollDirection: Axis.vertical,
                            shrinkWrap: true,
                            physics: ScrollPhysics(),
                            crossAxisCount: 3,
                            itemCount: snapshot.data.docs.length,
                            itemBuilder: (context, index) {
                              DocumentSnapshot video =
                                  snapshot.data.docs[index];

                              return  InkWell(
                                onTap: (){
                                   Navigator.of(context).pushNamed(
                          ChatFirstpage.route);
                          },child: Card(               
                                  elevation: 0.0,
                                  child: ClipRRect(
                                    borderRadius: BorderRadius.circular(25),
                                    clipBehavior: Clip.antiAliasWithSaveLayer,
                                    child: Image.network(
                                      video.data()['previewimage'],
                                      fit: BoxFit.cover,
                                    ),
                                  ),

                                  //imageData: searchImages[index],
                                ),
                              );
                            },
                            staggeredTileBuilder: (index) =>
                                StaggeredTile.count((index % 7 == 0) ? 2 : 1,
                                    (index % 7 == 0) ? 2 : 1),
                            mainAxisSpacing: 8.0,
                            crossAxisSpacing: 4.0,
                          );
                        },
                      ),

And heres my Videos collection

Future myVideos;
  int likes = 0;
  bool dataisthere = false;

  @override
  void initState() {
    super.initState();
    getalldata();
  }

  getalldata() async {
    //get videos as future
    myVideos = FirebaseFirestore.instance
        .collection('videos')
        .where('uid', isEqualTo: widget.userid)
        .get();
    var documents = await FirebaseFirestore.instance
        .collection('videos')
        .where('uid', isEqualTo: widget.userid)
        .get();
    for (var item in documents.docs) {
      likes = item.data()['likes'].length + likes;
    }
    setState(() {
      dataisthere = true;
    });
  }

You can do a check before navigating to the movie's page. Something like:

// ... other lines
          itemCount: snapshot.data.docs.length,
          itemBuilder: (context, index) {
            DocumentSnapshot video = snapshot.data.docs[index];

            return InkWell(
              onTap: () {

                // Do your check here, if video not exist display a SnackBar
                if (video.data()['video_url'] == null) {
                  Scaffold.of(context).showSnackBar(SnackBar(
                    content: Text('Video not exist'),
                    duration: Duration(seconds: 2),
                    backgroundColor: Colors.orange,
                  ));
                } else {

                  // If video exist, navigate to the page
                  Navigator.of(context).pushNamed(ChatFirstpage.route);
                }
              },
              child: Card(
                elevation: 0.0,
                child: ClipRRect(
// ... other lines

Try this:

DocumentSnapshot video =   snapshot.data.docs[index];

if (video.data()['previewimage'] == null) {return Text('no video');}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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