Getter uid being called on null in flutter fire

為{幸葍}努か 提交于 2020-02-07 02:35:07

问题


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.

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.

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

Code:

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,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                );
              },
            );
          }
        },
      ),
    );
  }
}

Error

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

回答1:


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.

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);
    }  
  }

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



来源:https://stackoverflow.com/questions/59910970/getter-uid-being-called-on-null-in-flutter-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!