Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Future builder for dropdown menu items #321

Closed
ramnath30 opened this issue Jun 15, 2020 · 3 comments
Closed

Future builder for dropdown menu items #321

ramnath30 opened this issue Jun 15, 2020 · 3 comments

Comments

@ramnath30
Copy link

Are there plans to allow a Future to populate the dropdown values? An example is retrieving a list of countries and states from a REST API and populating them in the dropdown menu. If this can already be done, please let me know how. Thanks for your help.

@FickleLife
Copy link

I am looking to do this too - any assistance would be great.

@moesaid
Copy link

moesaid commented Jun 19, 2020

you would need to wrap your FormBuilder in FutureBuilder
and then assign the Future list to normal list for ... for example

// normal to use a list
List _catsList;

// your future list
Future<List> _cats;
Future<List> _getCats() async{
    var res = await Dio().get('http://wazner.test/api/cat');
    return res.data;
}
  @override
  void initState() {
    _cats =  _getCats();
    super.initState();
  }
FutureBuilder(
   future: _cats,
    builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
       if (snapshot.hasData){                  
        _catsList = snapshot.data;

         return FormBuilder(
              // your logic
             // includes your dropdown menu items

            FormBuilderDropdown(
                 items: _catsList.map((item) => DropdownMenuItem(
                 value: item['id'],
                 child: Text("${item['name']}")
              )).toList(),
            ),

         )

        }
         return CircularProgressIndicator();
       },
 ),

@ramnath30
Copy link
Author

Thanks much for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants