我怎样才能在 flutter 中动态获取单个 firestore 文档
how can i get single firestore document dynamically in flutter
我在 listveiw.builder 小部件中显示了一个字符串列表 我想在按下单个单词时从 firebase 获取文档信息
var words = [red,blue,green,yellow] ;
我想根据列表的值获取文档
显示在:
ListView.builder(
itemCount: words.length,
itemBuilder: (context, index) {
return
Column(
children: [
Row(
children: [
Expanded(
flex: 3,
child: InkWell(
onTap: () {},
child: Text('${words[index]}'),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
// press this and get the data of {words[index]} document
},
child: Text(
'press to get color info',
),
),
),
],
),
Text( show the data here after fetching it from firestore)
],
);
},
)
您可以像这样使用文档 ID 阅读单个文档
class GetUserName extends StatelessWidget {
final String documentId;
GetUserName(this.documentId);
@override
Widget build(BuildContext context) {
CollectionReference users =FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
return Text("Full Name: ${data['full_name']} ${data['last_name']}");
}
return Text("loading");
},
);
}
}
有关详细信息,请访问 https://firebase.flutter.dev/docs/firestore/usage
假设您有一个 Firestore,其中包含名为 'words'
的集合
CollectionReference wordsFireStoreRef = FirebaseFirestore.instance.collection('words');
现在您必须访问集合中包含您要查找的词的文档。
ListView.builder(
itemCount: words.length,
itemBuilder: (context, index) {
return
Column(
children: [
Row(
children: [
Expanded(
flex: 3,
child: InkWell(
onTap: () {},
child: Text('${words[index]}'),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
// press this and get the data of {words[index]} document
// LOOK HERE. This line will return a type called Future<DocumentSnapshot>
wordsFireStoreRef.doc(words[index]).get();
},
child: Text(
'press to get color info',
),
),
),
],
),
Text( show the data here after fetching it from firestore)
],
);
},
)
剩下的就交给你了。您可以使用 FutureBuilder 构建一个小部件,或者您可以这样做
wordsFireStoreRef.doc(words[index]).get().then((value){
//Value is the data that was returned
print(value.data()['word']);
});
我在 listveiw.builder 小部件中显示了一个字符串列表 我想在按下单个单词时从 firebase 获取文档信息
var words = [red,blue,green,yellow] ;
我想根据列表的值获取文档
显示在:
ListView.builder(
itemCount: words.length,
itemBuilder: (context, index) {
return
Column(
children: [
Row(
children: [
Expanded(
flex: 3,
child: InkWell(
onTap: () {},
child: Text('${words[index]}'),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
// press this and get the data of {words[index]} document
},
child: Text(
'press to get color info',
),
),
),
],
),
Text( show the data here after fetching it from firestore)
],
);
},
)
您可以像这样使用文档 ID 阅读单个文档
class GetUserName extends StatelessWidget {
final String documentId;
GetUserName(this.documentId);
@override
Widget build(BuildContext context) {
CollectionReference users =FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
return Text("Full Name: ${data['full_name']} ${data['last_name']}");
}
return Text("loading");
},
);
} }
有关详细信息,请访问 https://firebase.flutter.dev/docs/firestore/usage
假设您有一个 Firestore,其中包含名为 'words'
的集合CollectionReference wordsFireStoreRef = FirebaseFirestore.instance.collection('words');
现在您必须访问集合中包含您要查找的词的文档。
ListView.builder(
itemCount: words.length,
itemBuilder: (context, index) {
return
Column(
children: [
Row(
children: [
Expanded(
flex: 3,
child: InkWell(
onTap: () {},
child: Text('${words[index]}'),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
// press this and get the data of {words[index]} document
// LOOK HERE. This line will return a type called Future<DocumentSnapshot>
wordsFireStoreRef.doc(words[index]).get();
},
child: Text(
'press to get color info',
),
),
),
],
),
Text( show the data here after fetching it from firestore)
],
);
},
)
剩下的就交给你了。您可以使用 FutureBuilder 构建一个小部件,或者您可以这样做
wordsFireStoreRef.doc(words[index]).get().then((value){
//Value is the data that was returned
print(value.data()['word']);
});