Как отобразить список карт в DropdownMenuItem во Flutter - PullRequest
1 голос
/ 25 июня 2019

Я новичок с Флаттером. Я хочу отобразить DropdownMenuItem из моей переменной списка. Проверьте мой код.


// my list variable


  List listUserType = [
    {'name': 'Individual', 'value': 'individual'},
    {'name': 'Company', 'value': 'company'}
  ];




// items property in DropdownMenuItem

return DropdownButtonFormField<List<Map>>(
        decoration: InputDecoration(
          prefixIcon: Icon(Icons.settings),
          hintText: 'Organisation Type',
          filled: true,
          fillColor: Colors.white,
          errorStyle: TextStyle(color: Colors.yellow),
        ),
        items: listUserType.map((map) {
          return DropdownMenuItem(
            child: Text(map['name']),
            value: map['value'],
          );
        }).toList());

Это результат, который я получил

I/flutter (22528): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (22528): The following assertion was thrown building RegisterPage(dirty, state: RegisterPageState#5b83a):
I/flutter (22528): type 'List<DropdownMenuItem<dynamic>>' is not a subtype of type
I/flutter (22528): 'List<DropdownMenuItem<List<Map<dynamic, dynamic>>>>'

Я не знаю, что является причиной ошибки.

Ответы [ 2 ]

1 голос
/ 25 июня 2019

Попробуйте удалить <List<Map>>

return DropdownButtonFormField(
        decoration: InputDecoration(
          prefixIcon: Icon(Icons.settings),
          hintText: 'Organisation Type',
          filled: true,
          fillColor: Colors.white,
          errorStyle: TextStyle(color: Colors.yellow),
        ),
        items: listUserType.map((map) {
          return DropdownMenuItem(
            child: Text(map['name']),
            value: map['value'],
          );
        }).toList());
0 голосов
/ 25 июня 2019

Измените DropdownButtonFormField<List<Map>> на DropdownButtonFormField<String> и добавьте параметр типа String к return DropdownMenuItem<String>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...