Flatter firebase firestore:&x27;path.isNotEmpty';:文档路径必须是非空字符串)
我正在尝试为我的颤振应用程序制作聊天应用程序。但每次我试图点击聊天时,都会出现这个错误。 有人能告诉我我的代码有什么问题吗Flatter firebase firestore:&x27;path.isNotEmpty';:文档路径必须是非空字符串),firebase,flutter,chat,Firebase,Flutter,Chat,我正在尝试为我的颤振应用程序制作聊天应用程序。但每次我试图点击聊天时,都会出现这个错误。 有人能告诉我我的代码有什么问题吗 readLocal() async { prefs = await SharedPreferences.getInstance(); id = prefs.getString('id') ?? ''; if (id.hashCode <= peerId.hashCode) { groupChatId = '$id-$peerId'; } else { grou
readLocal() async {
prefs = await SharedPreferences.getInstance();
id = prefs.getString('id') ?? '';
if (id.hashCode <= peerId.hashCode) {
groupChatId = '$id-$peerId';
} else {
groupChatId = '$peerId-$id';
}
FirebaseFirestore.instance
.collection('users')
.doc(id)
.update({'chattingWith': peerId});
setState(() {});
readLocal()异步{
prefs=等待SharedReferences.getInstance();
id=prefs.getString('id')??“”;
if(id.hashCodeif看起来您的id
变量是一个空字符串,它不是传递到doc()
的有效文档id
给定如何初始化id
:
id = prefs.getString('id') ?? '';
似乎prefs.getString('id')
返回空值。您可能想知道发生这种情况时要做什么,但一个简单的方法是检查:
id = prefs.getString('id');
if (!id.isEmpty) {
if (id.hashCode <= peerId.hashCode) {
groupChatId = '$id-$peerId';
} else {
groupChatId = '$peerId-$id';
}
FirebaseFirestore.instance
.collection('users')
.doc(id)
.update({'chattingWith': peerId});
setState(() {});
}
id=prefs.getString('id');
如果(!id.isEmpty){
如果(id.hashCode)我像你说的那样尝试。但是错误仍然是一样的。它说id.isEmpty返回null。你能显示准确的错误消息吗?发生了异常。NoSuchMethodError(NoSuchMethodError:在null上调用了getter'isEmpty'。接收方:null尝试调用:isEmpty)嗯…那以前也应该失败。但是如果id
也可以为null,你可以用if(id!=null&&!id.isEmpty){
或if(!id?.isEmpty){
检查。现在可以了!非常感谢