使用时会在firebase扑波
我们正在制作像Instagram这样的主页,我现在只想显示用户帖子。 代码就像这样
StreamBuilder(
stream: FirebaseFirestore.instance.collection('posts').orderBy("datePublished", descending: true).snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return PageView.builder(/*the rest of the code ...*/);
起作用,但它显示了所有帖子,所以我添加了此帖子
.where('uid', isEqualTo: uid)
,看起来像这样 内部小部件的内部构建
var uid = FirebaseAuth.instance.currentUser!.uid;
在身体内部
StreamBuilder(
stream: FirebaseFirestore.instance.collection('posts').where('uid', isEqualTo: uid).orderBy("datePublished", descending: true).snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return PageView.builder( //to make the page scroll
itemCount: snapshot.data!.docs.length,/*the rest of the code*/);
,并显示出线上的错误
itemcount:snapshot.data!.docs.length, 说
null检查操作员在空值上使用
数据库看起来像
we are making home page like Instagram and I want to show only the user post for now.
the code was like this
StreamBuilder(
stream: FirebaseFirestore.instance.collection('posts').orderBy("datePublished", descending: true).snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return PageView.builder(/*the rest of the code ...*/);
it worked but it shows all the post so I added this
.where('uid', isEqualTo: uid)
and it looks like this
inside Widget build
var uid = FirebaseAuth.instance.currentUser!.uid;
inside body like this
StreamBuilder(
stream: FirebaseFirestore.instance.collection('posts').where('uid', isEqualTo: uid).orderBy("datePublished", descending: true).snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return PageView.builder( //to make the page scroll
itemCount: snapshot.data!.docs.length,/*the rest of the code*/);
and it shows an error in line
itemCount: snapshot.data!.docs.length,
sayingNull check operator used on a null value
the database look like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误表示
snapshot.data
是null
。当snapshot.connectionState
等于Connection State.none
时,可能会发生这种情况。根据如何设置浏览量,您可以通过将该行更改为
itemcount:snapshot.data?.docs.length ?? 0,
当
snapshot.data
为null时,它将通过0。另一种方法是事先检查空值。
The error indicates that
snapshot.data
isnull
. This could happen whensnapshot.connectionState
is equal toConnectionState.none
when the stream is initialized and should be protected against.Depending on how the PageView is set up you could handle this by changing that line to
itemCount: snapshot.data?.docs.length ?? 0,
which will pass 0 when
snapshot.data
is null.Another way would be to check for the null value beforehand.