Flutter - Select list with display and return value

Viewed 330

Is it possible in flutter use select list with display and return value

For example:

from this web service https://jsonplaceholder.typicode.com/users

id = value in select list, name = display value in select list

If user select Leanne Graham I need get id of Leanne Graham (1) and send to database. If select Ervin Howell need get value (2).

1 Answers

Refer my answer here for that.

You want to create One class for Your all list and call this url for that

Your All list API call

 Future<List<dynamic>> getJobsData() async {
    String url = 'https://jsonplaceholder.typicode.com/users';
    var response = await http.get(Uri.parse(url), headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    });
    return json.decode(response.body);
  }

Your All list Widget:

 Expanded(
        child: FutureBuilder<List<dynamic>>(
          future: getJobsData(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: ListView.builder(
                  shrinkWrap: true,
                   itemCount: snapshot.data!.length,
                  itemBuilder: (context, index) {
                    var name = snapshot.data![index]['name'];
                    var email = snapshot.data![index]['email'];
                    var id = snapshot.data![index]['id'];
                    return Card(
                      shape: RoundedRectangleBorder(
                        side: BorderSide(color: Colors.green.shade300),
                        borderRadius: BorderRadius.circular(15.0),
                      ),
                      child: Column(
                        children: [
                          ListTile(
                            leading: Text(id.toString()),
                            title: Text(name),
                            subtitle: Text(email),
                          ),
                        ],
                      ),
                    );
                  },
                ),
              );
            }
            return CircularProgressIndicator();
          },
        ),
      ),
      

Your result screen for all data -> enter image description here

Then you create sent Id to that name to next class for selected user and use this url

you pass above selected user id this formate

Related