Home > Net >  Flutter Dropdown List Issue
Flutter Dropdown List Issue

Time:11-24

I have one phpmysql database. And i am fetching data from database. And i wanna showing my categories data into my dropdown button. But its showing only first data. Thats my code.

SizedBox(
                                width: context.dynamicWidth(5),
                                height: context.dynamicHeight(5),
                                child: ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: snap.length,
                                  itemBuilder: (context, index) {
                                    var categoryList =
                                        snap[index]['categoryName'];
                                    return DropdownButton<String>(
                                      items: <String>[
                                        categoryList,
                                      ].map((String value) {
                                        return DropdownMenuItem<String>(
                                          value: value,
                                          child: Text(value),
                                        );
                                      }).toList(),
                                      onChanged: (_) {},
                                    );
                                  },
                                ),
                              ),

Thats show only first data. Thx for helping.

CodePudding user response:

I don't know your snap list type but hope this work's

SizedBox(
                                width: context.dynamicWidth(5),
                                height: context.dynamicHeight(5),
                                child: ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: snap.length,
                                  itemBuilder: (context, index) {
                                    return DropdownButton<String>(
                                      items: snap.map((value) {
                                        return DropdownMenuItem<String>(
                                          value: value['categoryName'],
                                          child:Text(value['categoryName']),
                                        );
                                      }).toList(),
                                      onChanged: (_) {},
                                    );
                                  },
                                ),
                              ),

CodePudding user response:

Try

    var categoryList = snap[index];

instead of

    var categoryList = snap[index]['categoryName'];
  • Related