[英]Getter uid being called on null in flutter fire
I am trying to concatenate two UID's in order to create a chatroom.我正在尝试连接两个 UID 以创建聊天室。 One uid is being read from firebase while the other is read from the FirebaseAuth.instance.
一个 uid 是从 firebase 读取的,而另一个是从 FirebaseAuth.instance 读取的。
The clientUID is being assigned as it should, as I am passing it to another page on a Text widget.当我将它传递到 Text 小部件上的另一个页面时,clientUID 正在按应有的方式分配。 However the chatroom is not being created in the firestore tree so I assume this should be because of the instructor uid.
但是聊天室没有在 firestore 树中创建,所以我认为这应该是因为教师 uid。
Maybe I am not calling the FirebaseAuth.instance as it should?也许我没有像它应该的那样调用 FirebaseAuth.instance?
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
instructor
is a instance variable in the class ClientiPage
, thats why you can access it using the property widget
. instructor
是类ClientiPage
中的一个实例变量,这就是您可以使用属性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: uid
将检索当前登录的用户 ID,您不必在构造函数中或从其他屏幕传递它,因此您可以执行以下操作:
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
.只要用户登录,您就可以使用以下代码检索
uid
(await FirebaseAuth.instance.currentUser()).uid
。 There is no need to pass it from screen to screen.无需将其从屏幕传递到屏幕。
https://pub.dev/packages/firebase_auth https://pub.dev/packages/firebase_auth
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.