简体   繁体   English

实时颤动流构建器

[英]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实时颤动应用程序流构建器这是群聊应用程序我需要实时从firebase firestore获取布尔聊天值,如果真或假没有退出页面并重新打开以更新值或任何阻止用户发送消息的方式当 Chat 的值等于 False 时的组这是我的代码

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(),),

your code has the wrong implementation.你的代码有错误的实现。

  1. when providing the doc it should be the docId ({DOCID}) on firestore eg: _firestore.collection('admin').doc({DOCID}).snapshots()提供文档时,它应该是 firestore 上的 docId ({DOCID}),例如: _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我在代码中的任何地方都看不到您在哪里处理来自 firestore 的快照数据

check the flutter fire documentation for the correct implementation检查颤振火灾文档以获得正确的实施

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

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