简体   繁体   English

如何在flutter中添加多个具有firebase下拉值的streambuilder?

[英]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.

相关问题 Flutter - 如何使用 Firebase Auth 在 streambuilder 中访问未来的价值 - Flutter - how to access the value of a future inside a streambuilder, with Firebase Auth Flutter Firebase 多文档 ID StreamBuilder - Flutter Firebase Multiple Document Id StreamBuilder Flutter &amp; Firebase streamBuilder 分页 - Flutter & Firebase streamBuilder paginantion 如何使用 StreamBuilder 在 Flutter 中观察 Firebase 的变化 - How to observe Firebase changes in Flutter using StreamBuilder Flutter:带有 Firebase 的 StreamBuilder - Flutter: StreamBuilder with Firebase Flutter Firebase StreamBuilder如何只使用一个特定文件 - Flutter Firebase StreamBuilder how to use only one specific document Flutter 在使用 StreamBuilder 从 firebase 加载数据之前显示红色错误屏幕,如何解决? - Flutter show's a red error screen before loading data from firebase using StreamBuilder, how to fix? 将下拉列表 firebase 中的值分配给 flutter 中的 function - Assign value from dropdown list firebase to function in flutter 在Flutter中使用带有FireBase数据的StreamBuilder时,如何将ListView替换为GroupedListView? - How to replace ListView with GroupedListView while using StreamBuilder with FireBase data in Flutter? 如何在 Flutter 中将 Firebase 实时数据库读取为“字符串”(没有 Widget,streambuilder)? - How to read Firebase realtime database as "string"(without Widget, streambuilder) in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM