slovo coc
slovo coc

Reputation: 55

Flutter limit write operations to firestore

Am trying to limit the number of uploads a user can make to the database. To this I have a function that uses streambuilder to check the database if the number of files are greater than 5, if so its supposed to return a dialog. else its supposed to allow upload. However this is not working, when I tap the upload button nothing happens and no errors are highlighted. Here is my code for reference

    Stream stream;
    
    startstream(){
        setState(() {
          skillstream=FirebaseFirestore.instance
              .collection('posts')
              .doc(userid)
              .collection("UserPosts")
              .snapshots();
    
        });}
    @override
      void initState() {
        // TODO: implement initState
        startstream();
    
        super.initState();
      }
    
    checkskills()async{
        //check firestore for the number of  posts the user currently has active
    
       StreamBuilder<QuerySnapshot>(
           stream: stream,
       builder: (context, uploadcounter) {
         if (uploadcounter.data.docs.length > 5) {
           setState(() {
             showDialog(
                 context: context,
                 barrierDismissible: false,
                 builder:(BuildContext context){
                   return AlertDialog(
                     title: Text("Upload limit exceeded"),
                     content: SingleChildScrollView(
                       child: ListBody(children: [
                         Text("Limit Exceeded"),
                         Text("You already have 5 active posts. Delete some before posting more")
                       ],),
                     ),
                     actions: <Widget>[
                       TextButton(
                         child: Text('Close'),
                         onPressed: () {
                           Navigator.of(context).pop();
                         },
                       ),
                     ],
                   );
                 }
    
             );
    
           });
    
    
    
         }else{
           Navigator.pushNamed(context, '/upload');
           setState(() {
             _isOpened = !_isOpened;
           });
    
         }
       });}

Widget build(BuildContext context) {
return Scaffold(

body: Container(child:FlatButton(
                      child: Text("Upload"),
                      color: Colors.blue,
                      onPressed: ()async {
                        await checkskills
                      },
                    ),))

}

Upvotes: 0

Views: 41

Answers (2)

slovo coc
slovo coc

Reputation: 55

For anyone in future who might want to achieve something similar. I went the get() route instead of streambuilder and got it to work. i.e

Future checkuploads()async{
    //check firestore for the number of  posts the user currently has active
   return FirebaseFirestore.instance
       .collection('collecttionname')
       .doc(userid)
       .collection("othercollection")
   .get()
       .then((value) {
     var count = 0;
     count = value.docs.length;

if(count>=3){
//do something

}else{
//your upload function

}

//the call the above function where you need it


}

Upvotes: 0

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12363

Add 'return' before StreamBuilder, like this:

checkskills()async{

return StreamBuilder<QuerySnapshot>(
       stream: stream,
   builder: ///etc

Upvotes: 1

Related Questions