Dart closure return type problem after migrating to null safety
309
As suggested by jamesdlin, here's what worked for me at least syntactically for now. I'm not sure yet if it's going to work functionally - I haven't build the app yet as I'm still fixing null-safety migration issues.
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();
},
);
}
![Ernesto](https://lh6.googleusercontent.com/-FPwJJk7uwnM/AAAAAAAAAAI/AAAAAAAAARU/MQCiO08HPjo/photo.jpg?sz=256)
Author by
Ernesto
Updated on December 01, 2022Comments
-
Ernesto over 1 year
The below code used to work before migrating to Flutter 2 and null safety. I just can't figure out what the correct return type should be. The error I get is:
The return type 'List<PopupMenuItem<dynamic>?>' isn't a 'List<PopupMenuEntry<dynamic>>', as required by the closure's context.
And the code is:
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 }, ); }
-
Benyamin almost 3 yearsHere remove the type and see what happens Map<String, dynamic> itemMap = item....; =>itemMap = .... where have u declared PopupMenuEntry?
-
jamesdlin almost 3 yearsYour callback function contains an
if
statement, but it doesn't return a value along all code paths. It therefore implicitly returnsnull
if theif
-condition isn't met, causing your callback function to return aPopupMenuItem<dynamic>?
instead of aPopupMenuItem<dynamic>
as expected. Fix your code to return aPopupMenuItem
along all code paths, or use.where
or.whereType
if you want to filter the list. -
Ernesto almost 3 years@jamesdlin - Thanks for the advice. It worked! Now I just need to figure out what exactly I need to return for that specific path.
-