[英]Flutter Firebase return a Querysnapshot and DocumentSnapshot in the same widget
I've spent weeks trying to sort this issue and can't seem to sort it.我花了数周时间试图解决这个问题,但似乎无法解决。
I have a database with two collections userTable and userMoods我有一个包含两个 collections userTable 和 userMoods 的数据库
I have a future builder which is returning the name, however I am querying the userMood table to return the last created document.我有一个返回名称的未来生成器,但是我正在查询 userMood 表以返回最后创建的文档。
I cannot seem to find a way to get this data back out.我似乎无法找到恢复这些数据的方法。
Picture of data I am trying to retrieve:我要检索的数据图片:
Code is as follows:代码如下:
class CorrectMood extends StatefulWidget {
const CorrectMood({Key? key}) : super(key: key);
@override
_CorrectMoodState createState() => _CorrectMoodState();
}
class _CorrectMoodState extends State<CorrectMood> {
Future<DocumentSnapshot<Map<String, dynamic>>>? _fetchedData;
@override
void initState() {
super.initState();
_fetchedData = getData();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _fetchedData,
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.hasData) {
return Scaffold(
appBar: AppBar(
title: const Text('Display the Picture'),
backgroundColor: kPrimaryColor,
),
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
body: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 20.0, 8.0, 8.0),
child: Column(
children: [
Center(
child:
Text(
"${snapshot.data!.data()!["firstName"]} \n\n "
"We have predicted your mood as:\n\n "
//"${DatabaseService.getMood()}\n\n"
"Please select a reason associated to your mood",
style: const TextStyle(
color: Colors.black, fontSize: 15),
textAlign: TextAlign.center,
),
),
],
),
),
],
),
),
);
}else {
return CircularProgressIndicator();
}
},
);
}
}
Future<DocumentSnapshot<Map<String, dynamic>>> getData() async {
var currentUser = FirebaseAuth.instance.currentUser;
return await FirebaseFirestore.instance
.collection('USER_TABLE')
.doc(currentUser!.uid)
.get();
}
Future<QuerySnapshot<Map<String, dynamic>>> getMood() async {
var currentUser = FirebaseAuth.instance.currentUser;
return await FirebaseFirestore.instance
.collection('userMood')
.where('userId' == currentUser!.uid)
.orderBy('createdAt', descending: true)
.limit(1)
.get();
Any help is greatly appreciated!任何帮助是极大的赞赏!
you can use getData() directly to your FutureBuilder.您可以直接对您的 FutureBuilder 使用 getData()。 by the way I cannot where you are calling getMood() function.
顺便说一句,我不能在哪里调用 getMood() function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.