我正在尝试使用 Map(String,String) 来满足 FormBuilderChipOption。我尝试过的代码如下:
options:
languageOptionsMap((city,letter) => FormBuilderChipOption(
value: Text(city),
avatar: CircleAvatar(child: Text(letter)),
)).toList(growable: false),
The Map is as follows:
Map<String,String> languageOptionsMap = {
'English': 'E',
'Mandarin': 'M',
'Hindi': 'H',
'Spanish': 'S',
'Arabic': 'A',
'French': 'F',
'Swedish': 'S',
'Danish': 'D',
};
我收到如下编译时错误:
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building CompleteForm(dirty, state: _CompleteFormState#5d199):
'call'
Dynamic call of object has no instance method 'call'.
Receiver: Instance of 'IdentityMap<String, String>'
Arguments: [Instance of '(dynamic, dynamic) => FormBuilderChipOption<Text>']
The relevant error-causing widget was:
CompleteForm CompleteForm:file:///C:/Users/stefa/StudioProjects/appformbuilder/lib/main.dart:45:19
我期待获得 FormBuilderChipOption 的值和头像。
在 dart 中,你无法调用 Map (它不是函数或函数对象),因此你必须首先获取元素 ( .entries ),然后将它们映射 ( .map )只需要输入的函数,而不是(城市,字母)):
options: languageOptionsMap.entries.map((entry) => FormBuilderChipOption( value: Text(entry.key), avatar: CircleAvatar(child: Text(entry.value)), )).toList(growable: false),