[英]How to add multiple streambuilder that has dropdown value from firebase in flutter?
I have 3 dropdown that has lists from firebase. The flowchart of my program is when I choose the address, so the username and password are automatically selected according to the address that has been selected.我有 3 个下拉列表,其中包含来自 firebase 的列表。我的程序流程图是当我选择地址时,因此会根据已选择的地址自动选择用户名和密码。 This is my code:这是我的代码:
StreamBuilder 1流生成器 1
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('servers').snapshots(includeMetadataChanges: true),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: DropdownSearch<String>(
items: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
return data["address"];
})
.toList()
.cast<String>(),
onChanged: print,
),
);
},
),
StreamBuilder 2流生成器 2
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('servers').snapshots(includeMetadataChanges: true),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: DropdownSearch<String>(
items: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
return data["username"];
})
.toList()
.cast<String>(),
onChanged: print,
),
);
},
),
StreamBuilder 3流生成器 3
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('servers').snapshots(includeMetadataChanges: true),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: DropdownSearch<String>(
items: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
return data["password"];
})
.toList()
.cast<String>(),
onChanged: print,
),
);
},
),
And this is my firebase database:这是我的 firebase 数据库:
I've tried to write those code inside children: <Widget>[]
but the data can't showed in my app.我试图在children: <Widget>[]
中编写这些代码,但数据无法显示在我的应用程序中。
Thank you in advance for any help.预先感谢您的任何帮助。
Dropdowns are usually used as static data so if you are keeping that data in firebase. It would be better to fetch this data in a list in initState
method and then use in the StreamBuilder
instead of fetching it in StreamBuilder下拉列表通常用作 static 数据,因此如果您将该数据保存在 firebase 中。最好在initState
方法的列表中获取此数据,然后在 StreamBuilder 中使用,而不是在StreamBuilder
中获取
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.