简体   繁体   English

Firebase 数据库未在 flutter 应用程序中更新

[英]Firebase Database not updating in flutter app

I am working on a chatting app.我正在开发一个聊天应用程序。 I have done all the processes to install firebase and my app is working fine.我已经完成了安装 firebase 的所有过程,并且我的应用程序运行良好。 But when I connected the firebase database and tried to store the messages into the database.但是当我连接 firebase 数据库并尝试将消息存储到数据库中时。 The database does not update with the new document(message and user email).数据库不会使用新文档(消息和用户电子邮件)进行更新。

This is my code -这是我的代码 -

this is how my firebase database page looks like这就是我的 firebase 数据库页面的样子在此处输入图像描述

this is how my app looks like这就是我的应用程序的样子在此处输入图像描述

import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class ChatScreen extends StatefulWidget {
  static const String id = 'chat_screen';

  @override _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  final _firestore = Firestore.instance;
  String messageText;
  final _auth = FirebaseAuth.instance;
  FirebaseUser loggedinUser;

  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser();
      if (user != null) {
        loggedinUser = user;
        print(user.email);
      }
    } catch (e) {
      print(e);
    }
  }

  @override Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: null,
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.close),
              onPressed: () {
                _auth.signOut();
                Navigator.pop(context);
                //Implement logout functionality
              }),
        ],
        title: Text('⚡️Chat'),
        backgroundColor: Colors.lightBlueAccent,
      ),
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Container(
              decoration: kMessageContainerDecoration,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      onChanged: (value) {
                        messageText = value;
                        //Do something with the user input.
                      },
                      decoration: kMessageTextFieldDecoration,
                    ),
                  ),
                  FlatButton(
                    onPressed: () {
                      _firestore.collection('messages').add({
                        'sender': loggedinUser.email,
                        'text': messageText,
                      });
                      //Implement send functionality.
                    },
                    child: Text(
                      'Send',
                      style: kSendButtonTextStyle,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

** **

It seems that the problem is you're trying to add data with null values, and that's why you don't see any changes.似乎问题在于您正在尝试使用null值添加数据,这就是您看不到任何更改的原因。

The values that seems to be null are loggedinUser.email and messageText , check if the user has an email and for the messageText , in onChanged you should do messageText = value;似乎是null的值是loggedinUser.emailmessageText ,检查用户是否有emailmessageText ,在onChanged你应该做messageText = value; inside a setState() , but instead of doing that you could add a controller to the TextField and then use controller.text .setState()内,但不是这样做,您可以将controller添加到TextField ,然后使用controller.text

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

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