gpt4 book ai didi

firebase - documentSnapshot.data()不会传递给构建器:Flutter

转载 作者:行者123 更新时间:2023-12-03 03:20:07 25 4
gpt4 key购买 nike

我一直在尝试从/ users集合中获取文档数据,
但是问题是我声明了de builder之后:
这是FutureBuiulder的必需参数,数据未通过它,并且我得到data = null。
我究竟做错了什么?

future: FirebaseFirestore.instance
.collection('users')
.doc(uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
print('The Documents Exists');
print(documentSnapshot.data());
}
else {
print('The document Do not Exist');
}
} //here closes then((DocumentSnapshot documentSnapshot) {
),// here closesthen((DocumentSnapshot documentSnapshot)
我得到的文档正确无误,如下图所示
但是,数据不会在生成器之后传递:
builder: (context, documentSnapshot) {
if (documentSnapshot.hasData) {
print('The Documents Exists');
print(documentSnapshot.data());
}
else {
print('The document Do not Exist');
}
enter image description here
编辑
这是我的完整代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class NewHome extends StatefulWidget {
@override
_NewHomeState createState() => _NewHomeState();
}

class _NewHomeState extends State<NewHome> {
FirebaseFirestore firestore = FirebaseFirestore.instance;
FirebaseAuth auth = FirebaseAuth.instance;

@override
Widget build(BuildContext context) {
final User user = auth.currentUser;
final uid = user.uid;
return new Scaffold(
body: new Container(
child: FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.doc('uid')
.get(),


builder: (context, snapshot) {
DocumentSnapshot manuais =
snapshot.data;
if (snapshot.hasData) {
print('okkk');
}
else{
print('nopeeeee');
}

return Container(
padding: EdgeInsets.all(16),
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, uid) {
return Card(
color: Colors.grey[250],
child: Container(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
new Image.asset(
'Images/pdflogo.png',
width: 32,
),
Center(
child: Text(
(snapshot
.data()['nome']
.toString()),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 16),
),
),
])));
}));
})));
}
}

最佳答案

不要将thenFutureBuilder一起使用
将您的async方法直接传递给future,然后只有它解析为快照,您才能访问hasDatahasError getters之类的不同状态。

FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.doc("UID")
.get(),
builder: (context, snapshot) {
if(snapshot.hasData){
/// documentSnapshot.data()
}
return Container();
}
);

关于firebase - documentSnapshot.data()不会传递给构建器:Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63985064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com