Search code examples
flutterdropdowndropdownbutton

Flutter, DropdownButton. The value of the object is saved, but the selected object changes only then


DropdownButton:

DropdownButton(
                value: category,
                isExpanded: true,
                items: _journalsCategory.map<DropdownMenuItem<int>>((e) {
                  return DropdownMenuItem
                    (
                    child: Text(
                        e["name"],
                    ),
                    value: e["id"],
                    );
                }).toList(),
                onChanged: (t) {
                  setState(() {
                    category = t!;
                    MediaQuery.of(context).viewInsets.bottom;
                  });
                },
              ),

_journalsCategory:

List<Map<String, dynamic>> _journalsCategory = await SQLHelper().getCategory();

getCategory() from SQLHelper.dart:

Future<List<Map<String, dynamic>>?> getCategory() async {
    final Database? db = await database;
    return db?.rawQuery('SELECT name, id FROM categories');
  }

I've read a lot of documentation and I don't understand how to solve this problem. I need an id to be received when selecting an object because it is passed to the function as an int, so I prescribed DropdownMenuItem. The value is returned, but the name of the selected object is not updated.


Solution

  • I replaced DropDownButton with DropdownButtonFormField