'flutter streambuilder in real time

flutter application streambuilder in real time this is group chat app I need to get bool chat vale from firebase firestore if true or false in real time without exit page and re open to update the value Or any way to prevent the user from sending a message in the group when the value of Chat is equal to False this is my code

StreamBuilder(builder: (context, snapshot) {
        if(chat == false){
          return  Container(
              child:
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 30,vertical: 5),
                child: Row(
                  children: [
                    Image.network('https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Error.svg/1200px-Error.svg.png',height: 25,width: 25,),
                    SizedBox(width: 20,),
                    Expanded(child: Text('Only admin can send message',style: TextStyle(fontSize: 10),maxLines: 1,overflow: TextOverflow.ellipsis,))
                  ],
                ),
              )
          );
        }else{
          return Padding(
            padding: const EdgeInsets.all(10.0),
            child: Material(
              borderRadius: BorderRadius.circular(50),
              color: ColorConstants.appColor,
              child: Padding(
                padding: const EdgeInsets.only(bottom: 4.0,top: 4.0,left: 1.5,right: 1.5),
                child: Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(50)
                    ),
                    child:
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Expanded(child: TextField(
                          controller: messageedit,
                          onChanged: (value){
                            messageText = value;
                          },
                          decoration: InputDecoration(
                              contentPadding: EdgeInsets.symmetric(
                                  vertical: 10,
                                  horizontal: 20
                              ),
                              hintText: 'Write your message .... ',
                              border: InputBorder.none
                          ),
                        )),
                        IconButton(onPressed: ()  {
                          messageedit.clear();
                          _firestore.collection("messages").add({
                            'text': messageText,
                            'sender' : email,
                            'time' : FieldValue.serverTimestamp(),
                          }).whenComplete(() => sendNotification('$messageText', '$email'));
                        }, icon: Icon(Icons.send_rounded,color: ColorConstants.appColor,))
                      ],
                    )
                ),
              ),
            ),
          );
        }
      },stream:  _firestore.collection('admin').doc('admin').snapshots(),),


Solution 1:[1]

your code has the wrong implementation.

  1. when providing the doc it should be the docId ({DOCID}) on firestore e.g.: _firestore.collection('admin').doc({DOCID}).snapshots()
  2. i don't get how do you create the chat variable
  3. i don't see anywhere in the code where do you process snapshot data from firestore

check the flutter fire documentation for the correct implementation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fpatelm