迁移到 null safety 后的 dart closure return 类型问题
Dart closure return type problem after migrating to null safety
以下代码在迁移到 Flutter 2 和 null safety 之前可以正常工作。我只是不知道正确的 return 类型应该是什么。我得到的错误是:
The return type 'List<PopupMenuItem<dynamic>?>' isn't a 'List<PopupMenuEntry<dynamic>>', as required by the closure's context.
代码是:
Widget _parentMenuSelector() {
return PopupMenuButton<dynamic>(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.content_copy),
Text('Copy to another menu'),
],
),
),
onSelected: (dynamic selectedMenu) {
Map<String, dynamic> selMenu = selectedMenu;
print(selMenu['id']);
model!.copyMenuItem(selMenu['id']);
},
itemBuilder: (BuildContext context) {
return model!.restaurant!.restaurantMenus!.values.map((dynamic item) { //ERROR
Map<String, dynamic> itemMap = item; //ERROR
if (itemMap['name'] != model!.menu!.name) { //ERROR
return PopupMenuItem<dynamic>( //ERROR
child: Text('Copy to ${itemMap['name']}'), //ERROR
value: item, //ERROR
); //ERROR
} //ERROR
}).toList(); //ERROR
},
);
}
正如 jamesdlin 所建议的,这至少在语法上对我有用。我还不确定它是否会正常工作 - 我还没有构建应用程序,因为我仍在修复 null-safety 迁移问题。
Widget _parentMenuSelector() {
return PopupMenuButton<dynamic>(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.content_copy),
Text('Copy to another menu'),
],
),
),
onSelected: (dynamic selectedMenu) {
Map<String, dynamic> selMenu = selectedMenu;
print(selMenu['id']);
model!.copyMenuItem(selMenu['id']);
},
itemBuilder: (BuildContext context) {
return model!.restaurant!.restaurantMenus!.values.map((dynamic item) {
Map<String, dynamic> itemMap = item;
if (itemMap['name'] != model!.menu!.name) {
return PopupMenuItem<dynamic>(
child: Text('Copy to ${itemMap['name']}'),
value: item,
);
} else {
return PopupMenuItem<dynamic>(
child: null,
);
}
}).toList();
},
);
}
以下代码在迁移到 Flutter 2 和 null safety 之前可以正常工作。我只是不知道正确的 return 类型应该是什么。我得到的错误是:
The return type 'List<PopupMenuItem<dynamic>?>' isn't a 'List<PopupMenuEntry<dynamic>>', as required by the closure's context.
代码是:
Widget _parentMenuSelector() {
return PopupMenuButton<dynamic>(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.content_copy),
Text('Copy to another menu'),
],
),
),
onSelected: (dynamic selectedMenu) {
Map<String, dynamic> selMenu = selectedMenu;
print(selMenu['id']);
model!.copyMenuItem(selMenu['id']);
},
itemBuilder: (BuildContext context) {
return model!.restaurant!.restaurantMenus!.values.map((dynamic item) { //ERROR
Map<String, dynamic> itemMap = item; //ERROR
if (itemMap['name'] != model!.menu!.name) { //ERROR
return PopupMenuItem<dynamic>( //ERROR
child: Text('Copy to ${itemMap['name']}'), //ERROR
value: item, //ERROR
); //ERROR
} //ERROR
}).toList(); //ERROR
},
);
}
正如 jamesdlin 所建议的,这至少在语法上对我有用。我还不确定它是否会正常工作 - 我还没有构建应用程序,因为我仍在修复 null-safety 迁移问题。
Widget _parentMenuSelector() {
return PopupMenuButton<dynamic>(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.content_copy),
Text('Copy to another menu'),
],
),
),
onSelected: (dynamic selectedMenu) {
Map<String, dynamic> selMenu = selectedMenu;
print(selMenu['id']);
model!.copyMenuItem(selMenu['id']);
},
itemBuilder: (BuildContext context) {
return model!.restaurant!.restaurantMenus!.values.map((dynamic item) {
Map<String, dynamic> itemMap = item;
if (itemMap['name'] != model!.menu!.name) {
return PopupMenuItem<dynamic>(
child: Text('Copy to ${itemMap['name']}'),
value: item,
);
} else {
return PopupMenuItem<dynamic>(
child: null,
);
}
}).toList();
},
);
}