I got an error when I tried to use List.firstWhere and set orElse to return null
Error shows: The return type 'Null' isn't a 'City', as required by the closure's context
Sample code below
/// city.dart
class City {
final int id;
final String no;
final String name;
final String website;
final bool status;
City(this.id, this.no, this.name, this.website, this.status);
City.fromJson(Map<String, dynamic> json)
: id = json['id'],
no = json['no'],
name = json['name'],
website = json['website'],
status = json['status'];
Map<String, dynamic> toJson() =>
{'id': id, 'no': no, 'name': name, 'website': website, 'status': status};
}
/// main.dart
/// declare a list variable
List<City> _cities = [];
...
_cities.firstWhere((element) => element.id == 1, orElse: () => null); // error here
Though I can use firstWhereOrNull method in package:collection and won't get any error, I want to figure out how to use firstWhere in a correct way.
Thanks for help!