Home > Software engineering >  Assign value from dropdown list firebase to function in flutter
Assign value from dropdown list firebase to function in flutter

Time:02-01

I have a dropdown that has lists from firebase. Now I want the selected list assign to this function. How to do that? Thanks in advance for any help.

The function:

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

And this is my streamBuilder code:

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

CodePudding user response:

On onChanged method you need to save the actual data. And then assign it to your 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;
    },
      );
   },
),
  • Related