根据来自 firestore 的文档查询 collection 创建列表
create the list from the query collection of documents from firestore
我能够在 flutter 应用程序中成功地从 firestore collection 中获取文档并创建列表视图构建器。
除此之外,我想创建一个特定字段的列表,该列表对 collection.
中的所有文档都是通用的
例如,每个文档都有 username
的公共字段,在从 firestore collection 获取所有文档后,我想创建一个用户名列表 List usernames = [];
,我可以在其中添加 collection.
中的所有用户名
Query query2 = FirebaseFirestore.instance
.collection('users');
StreamBuilder<QuerySnapshot>(
stream: query2.snapshots(),
builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index){
return ListTile(title:Text(snapshot.data.docs[index]['username'] ?? "",
subtitle:Text(snapshot.data.docs[index]['email'] ?? ""
),);
});
}
)
上面的代码工作正常我想创建一个单独的列表用于用户名 List usernames = [];
我该怎么做?请指教
var usernames = snapshot.data.docs.map((e) => e['username']);
试试这个:
查询query2 = FirebaseFirestore.instance
.collection('users').where("用户名",isNull: false );
StreamBuilder(
流:query2.snapshots(),
builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index){
return ListTile(title:Text(snapshot.data.docs[index]['username'] ?? "",
subtitle:Text(snapshot.data.docs[index]['email'] ?? ""
),);
});
}
)
这基本上是检查整个集合和 returns username
不为空的数据。
我能够在 flutter 应用程序中成功地从 firestore collection 中获取文档并创建列表视图构建器。 除此之外,我想创建一个特定字段的列表,该列表对 collection.
中的所有文档都是通用的例如,每个文档都有 username
的公共字段,在从 firestore collection 获取所有文档后,我想创建一个用户名列表 List usernames = [];
,我可以在其中添加 collection.
Query query2 = FirebaseFirestore.instance
.collection('users');
StreamBuilder<QuerySnapshot>(
stream: query2.snapshots(),
builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index){
return ListTile(title:Text(snapshot.data.docs[index]['username'] ?? "",
subtitle:Text(snapshot.data.docs[index]['email'] ?? ""
),);
});
}
)
上面的代码工作正常我想创建一个单独的列表用于用户名 List usernames = [];
我该怎么做?请指教
var usernames = snapshot.data.docs.map((e) => e['username']);
试试这个:
查询query2 = FirebaseFirestore.instance .collection('users').where("用户名",isNull: false );
StreamBuilder( 流:query2.snapshots(),
builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index){
return ListTile(title:Text(snapshot.data.docs[index]['username'] ?? "",
subtitle:Text(snapshot.data.docs[index]['email'] ?? ""
),);
});
}
)
这基本上是检查整个集合和 returns username
不为空的数据。