扑灭火焰中的吸气剂uid被调用为null [英] Getter uid being called on null in flutter fire

查看:103
本文介绍了扑灭火焰中的吸气剂uid被调用为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将两个UID连接起来以创建聊天室.正在从Firebase读取一个uid,而从FirebaseAuth.instance读取另一个.

I am trying to concatenate two UID's in order to create a chatroom. One uid is being read from firebase while the other is read from the FirebaseAuth.instance.

正在将clientUID分配给它,因为我正在将其传递到Text小部件上的另一个页面.但是,聊天室不是在Firestore树中创建的,因此我认为这应该是由于讲师uid造成的.

The clientUID is being assigned as it should, as I am passing it to another page on a Text widget. However the chatroom is not being created in the firestore tree so I assume this should be because of the instructor uid.

也许我没有按应有的方式调用FirebaseAuth.instance?

Maybe I am not calling the FirebaseAuth.instance as it should?

代码:

class ClientiPage extends StatefulWidget {
  static const String id = 'CLIENTI';

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

class _ClientiPageState extends State<ClientiPage> {

  String chatRoomID;
  String clientUID;

  Firestore db = Firestore.instance;

  String instructor;

  void getInstructorId() async {
    instructor = (await FirebaseAuth.instance.currentUser()).uid;
  }

  void saveChatRoom() {

    getInstructorId();

    DocumentReference chatroomIdRef = db.collection('instructori').document(instructor).collection("chatrooms").document(chatRoomID);

    if (chatroomIdRef == null) {
      db.collection('instructori').document(instructor).collection("chatrooms").document(chatRoomID);
    }  
  }

  void createChatRoom() {
    getInstructorId();
    chatRoomID = clientUID + instructor;

    if(chatRoomID != null) {
      saveChatRoom();

      Navigator.push(
        context, 
        MaterialPageRoute(
          builder: (context) => ChatPage(
            chatRoomID: chatRoomID,
            clientUID: clientUID,
          ),
        ),
      );
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: db.collection('clienti').snapshots(),
        builder: (context, snapshot) {
          if (snapshot.data == null) {
            return Center(
              child: CircularProgressIndicator(),
            );
          } else {
            return ListView.builder(      
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                clientUID = snapshot.data.documents[index]["UID"];
                return Column(
                  children: <Widget>[
                    Divider(
                      height: 10.0,
                    ),
                    new ListTile(
                      onTap: createChatRoom,
                      title: new Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          new Text(
                            snapshot.data.documents[index]["numar_telefon"],
                            style: new TextStyle(
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                );
              },
            );
          }
        },
      ),
    );
  }
}

错误

The getter 'uid' was called on null.
Receiver: null
Tried calling: uid

推荐答案

instructor是类ClientiPage中的一个实例变量,这就是为什么您可以使用属性widget来访问它的原因.但是看来您没有正确初始化它.

instructor is a instance variable in the class ClientiPage, thats why you can access it using the property widget. But it seems you are not initializing it correctly.

uid将检索当前登录的用户ID,您不必在构造函数中或从其他屏幕传递它,因此您可以执行以下操作:

The uid will retrieve the currently logged in user id, you dont have to pass it inside a constructor or from a different screen, therefore you can do the following:

 void saveChatRoom() async {
    String userId  = (await FirebaseAuth.instance.currentUser()).uid;
    DocumentReference chatroomIdRef = db.collection('instructori').document(userId).collection("chatrooms").document(chatRoomID);

    if (chatroomIdRef == null) {
      db.collection('instructori').document(userId).collection("chatrooms").document(chatRoomID);
    }  
  }

只要用户已登录,就可以使用以下代码(await FirebaseAuth.instance.currentUser()).uid检索uid.无需在屏幕之间传递它.

As long as the user is logged in, you can retrieve the uid using the following code (await FirebaseAuth.instance.currentUser()).uid. There is no need to pass it from screen to screen.

https://pub.dev/packages/firebase_auth

这篇关于扑灭火焰中的吸气剂uid被调用为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆