简体   繁体   English

将下拉列表 firebase 中的值分配给 flutter 中的 function

[英]Assign value from dropdown list firebase to function in flutter

I have a dropdown that has lists from firebase. Now I want the selected list assign to this function. How to do that?我有一个下拉列表,其中包含来自 firebase 的列表。现在我希望将选定的列表分配给这个 function。该怎么做? Thanks in advance for any help.在此先感谢您的帮助。

The function: function:

FlutterVpn.connectIkev2EAP(
   server: _addressController.text,
   username: _usernameController.text,
   password: _passwordController.text,
);

And this is my streamBuilder code:这是我的 streamBuilder 代码:

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,
         ),
      );
   },
),

On onChanged method you need to save the actual data.onChanged方法上,您需要保存实际数据。 And then assign it to your function:然后将其分配给您的 function:

var dataAddress;

[...]

FlutterVpn.connectIkev2EAP(
// here use it
   server: dataAddress,
   username: _usernameController.text,
   password: _passwordController.text,
);

[...]

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: (var data) {
    // here save the info
      dataAddress = data;
    },
      );
   },
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM