如何从 firebase 中提取数据并将其用于提取另一个数据以在 StreamBuilder 中使用它
How to pull a data from firebase and use it for pulling another data to using it in StreamBuilder
我正在尝试从左侧获取当前登录用户的所有 postID,并且我想使用此信息来获取包含相同 postID 的所有帖子。我怎样才能做到这一点?
到目前为止,我创建了一个可以提取当前用户的所有 postID 并将其放入列表中的函数,但我认为我无法使用它。
https://i.stack.imgur.com/ThfSm.png
List<String> myFavoritePosts = [];
Future<List<String>> getFavorites() async {
final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = auth.currentUser;
final userID = user!.uid;
var result = await FirebaseFirestore.instance
.collection('favorites')
.where("userID", whereIn: [userID]).get();
for (var res in result.docs) {
myFavoritePosts.add((res.data()['postID'] ?? ''));
}
return myFavoritePosts;
}
但我无法将它实现到我的 StreamBuilder 结构中。
Widget build(BuildContext context) {
final Stream<QuerySnapshot> _postStream = FirebaseFirestore.instance
.collection('Posts')
.where('postID',
isEqualTo:
getFavorites())
.snapshots();
return StreamBuilder<QuerySnapshot>(
stream: _postStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Please try again');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return ListTile(
title: Text(data['title']),
subtitle: Column(
children: <Widget>[
Text(data['details']),
],
),
);
}).toList(),
);
如何连接这两个数据库以便显示具有相同 ID 的帖子?
使用这个:-
`
List<String> myFavoritePosts = [];
Future<List<String>> getFavorites() async {
var favourite;
final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = auth.currentUser;
final userID = user!.uid;
var result = await FirebaseFirestore.instance
.collection('favorites')
.where("userID", whereIn: [userID]).get();
for (var res in result.docs) {
myFavoritePosts.add((res.data()['postID'] ?? ''));
}
myFavoritePosts.forEach((element){
favourite = element;
});
return favourite;
}`
然后:-
'Widget build(BuildContext context) {
final Stream<QuerySnapshot> _postStream = FirebaseFirestore.instance
.collection('Posts')
.where('postID',
isEqualTo:
getFavorites())
.snapshots();
return StreamBuilder<QuerySnapshot>(
stream: _postStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Please try again');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return ListTile(
title: Text(data['title']),
subtitle: Column(
children: <Widget>[
Text(data['details']),
],
),
);
}).toList(),
);'
我正在尝试从左侧获取当前登录用户的所有 postID,并且我想使用此信息来获取包含相同 postID 的所有帖子。我怎样才能做到这一点?
到目前为止,我创建了一个可以提取当前用户的所有 postID 并将其放入列表中的函数,但我认为我无法使用它。 https://i.stack.imgur.com/ThfSm.png
List<String> myFavoritePosts = [];
Future<List<String>> getFavorites() async {
final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = auth.currentUser;
final userID = user!.uid;
var result = await FirebaseFirestore.instance
.collection('favorites')
.where("userID", whereIn: [userID]).get();
for (var res in result.docs) {
myFavoritePosts.add((res.data()['postID'] ?? ''));
}
return myFavoritePosts;
}
但我无法将它实现到我的 StreamBuilder 结构中。
Widget build(BuildContext context) {
final Stream<QuerySnapshot> _postStream = FirebaseFirestore.instance
.collection('Posts')
.where('postID',
isEqualTo:
getFavorites())
.snapshots();
return StreamBuilder<QuerySnapshot>(
stream: _postStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Please try again');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return ListTile(
title: Text(data['title']),
subtitle: Column(
children: <Widget>[
Text(data['details']),
],
),
);
}).toList(),
);
如何连接这两个数据库以便显示具有相同 ID 的帖子?
使用这个:-
`
List<String> myFavoritePosts = [];
Future<List<String>> getFavorites() async {
var favourite;
final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = auth.currentUser;
final userID = user!.uid;
var result = await FirebaseFirestore.instance
.collection('favorites')
.where("userID", whereIn: [userID]).get();
for (var res in result.docs) {
myFavoritePosts.add((res.data()['postID'] ?? ''));
}
myFavoritePosts.forEach((element){
favourite = element;
});
return favourite;
}`
然后:-
'Widget build(BuildContext context) {
final Stream<QuerySnapshot> _postStream = FirebaseFirestore.instance
.collection('Posts')
.where('postID',
isEqualTo:
getFavorites())
.snapshots();
return StreamBuilder<QuerySnapshot>(
stream: _postStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Please try again');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return ListTile(
title: Text(data['title']),
subtitle: Column(
children: <Widget>[
Text(data['details']),
],
),
);
}).toList(),
);'