Firebase 吸气剂';uid';被调用为null-但只有一半的时间
因此,我的颤振应用程序中有一个屏幕,它应该显示特定用户的所有注释。我将Firestore结构化,这样就有了一个注释集合,每个用户都有一个名为uid的文档。然后,他们的所有注释都存储在文档下的集合(usernotes)中 我在这里遇到的问题是,当您尝试访问应用程序中的notes页面时,会出现错误 对null调用了getter“uid”。 收件人:空 已尝试呼叫:uid 但是,当我简单地从颤振应用程序中单击run时,一切都正常。你可以在屏幕上看到所有的注释。这是我的笔记屏幕Firebase 吸气剂';uid';被调用为null-但只有一半的时间,firebase,flutter,dart,firebase-authentication,Firebase,Flutter,Dart,Firebase Authentication,因此,我的颤振应用程序中有一个屏幕,它应该显示特定用户的所有注释。我将Firestore结构化,这样就有了一个注释集合,每个用户都有一个名为uid的文档。然后,他们的所有注释都存储在文档下的集合(usernotes)中 我在这里遇到的问题是,当您尝试访问应用程序中的notes页面时,会出现错误 对null调用了getter“uid”。 收件人:空 已尝试呼叫:uid 但是,当我简单地从颤振应用程序中单击run时,一切都正常。你可以在屏幕上看到所有的注释。这是我的笔记屏幕 import 'pack
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'welcome_screen.dart';
class NoteScreen extends StatefulWidget {
static const String id = 'note_screen';
@override
_NoteScreenState createState() => _NoteScreenState();
}
class _NoteScreenState extends State<NoteScreen> {
final _auth = FirebaseAuth.instance;
User loggedInUser;
@override
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
try {
final user = await _auth.currentUser;
if (user != null) {
loggedInUser = user;
}
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Field Notes'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.chat),
tooltip: 'Messages',
onPressed: () {},
),
IconButton(
icon: const Icon(Icons.exit_to_app),
tooltip: 'Log Out',
onPressed: () {
_auth.signOut();
Navigator.pushNamed(context, WelcomeScreen.id);
},
),
],
),
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('notes').doc(loggedInUser.uid).collection('usernotes')
.snapshots(),
builder: (ctx, streamSnapShot) {
if(!streamSnapShot.hasData) return const Text('Loading...');
if (streamSnapShot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
final noteData = streamSnapShot.data.docs;
return ListView.builder(
itemCount: noteData.length,
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8),
child: Text(noteData[index]['text']),
),
);
},
),
floatingActionButton:
FloatingActionButton(child: Icon(Icons.add), onPressed: () {
FirebaseFirestore.instance.collection('notes').doc(loggedInUser.uid).collection('usernotes').add({
'text' : 'This was added by clicking the button!'
});
}),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:cloud_firestore/cloud_firestore.dart”;
导入“包:firebase_auth/firebase_auth.dart”;
导入“welcome_screen.dart”;
类NoteScreen扩展StatefulWidget{
静态常量字符串id='note_screen';
@凌驾
_NoteScreenState createState()=>\u NoteScreenState();
}
类_NoteScreenState扩展状态{
final _auth=FirebaseAuth.instance;
用户日志用户;
@凌驾
void initState(){
super.initState();
getCurrentUser();
}
void getCurrentUser()异步{
试一试{
最终用户=等待_auth.currentUser;
如果(用户!=null){
loggedInUser=用户;
}
}捕获(e){
印刷品(e);
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:常量文本(“字段注释”),
行动:[
图标按钮(
图标:常量图标(Icons.chat),
工具提示:“消息”,
按下:(){},
),
图标按钮(
图标:常量图标(图标。退出到应用程序),
工具提示:“注销”,
已按下:(){
_auth.signOut();
Navigator.pushNamed(上下文,WelcomeScreen.id);
},
),
],
),
正文:StreamBuilder(
流:FirebaseFirestore.instance
.collection('notes').doc(loggedInUser.uid).collection('usernotes'))
.snapshots(),
生成器:(ctx、streamSnapShot){
如果(!streamSnapShot.hasData)返回常量文本('Loading…');
if(streamSnapShot.connectionState==connectionState.waiting){
返回中心(
子对象:CircularProgressIndicator(),
);
}
final noteData=streamSnapShot.data.docs;
返回ListView.builder(
itemCount:noteData.length,
itemBuilder:(ctx,索引)=>容器(
填充:边缘设置。全部(8),
子项:Text(noteData[index]['Text']),
),
);
},
),
浮动操作按钮:
FloatingActionButton(子:图标(Icons.add)),ON按下:(){
FirebaseFirestore.instance.collection('notes').doc(loggedInUser.uid)。collection('usernotes')。添加({
“text”:“这是通过单击按钮添加的!”
});
}),
);
}
}
当前用户
属于用户
类型,因此不需要使用等待
,因为它不会返回未来
。您可以执行以下操作:
FirebaseFirestore.instance.collection('notes').doc(_auth.currentUser.uid).collection('usernotes').snapshots(),
以及:
currentUser
属于User
类型,因此您不需要使用wait
,因为它不会返回未来
。您可以执行以下操作:
FirebaseFirestore.instance.collection('notes').doc(_auth.currentUser.uid).collection('usernotes').snapshots(),
以及:
在调用await\u auth.currentUser之前调用了Build方法。这就是在null上调用loggedInUser.uid的原因。我不明白它是如何工作的。我的await\u auth.currentUser位于构建方法之上。根据firebase auth的更新插件,您不需要使用await获取当前用户,只需使用\u auth.currentUser.uid即可。如果您有最新版本的firebase auth。不知道为什么这个问题被否决。我如何改进这个问题?我的问题出了什么问题?在调用await\u auth.currentUser之前调用了Build方法。这就是在null上调用loggedInUser.uid的原因。我不明白它是如何工作的。我的await\u auth.currentUser位于构建方法之上。根据firebase auth的更新插件,您不需要使用await获取当前用户,只需使用\u auth.currentUser.uid即可。如果您有最新版本的firebase auth。不知道为什么这个问题被否决。我如何改进这个问题?我的问题怎么了?