class Destination {
String imageUrl;
String city;
String country;
String description;
List<Activity> activities;
Destination({
this.imageUrl,
this.city,
this.country,
this.description,
this.activities,
});
List<Activity> activities = [
Activity(
imageUrl: 'assets/images/stmarksbasilica.jpg',
name: 'St. Mark\'s Basilica',
type: 'Sightseeing Tour',
startTimes: ['9:00 am', '11:00 am'],
rating: 5,
price: 30,
),
CodePudding user response:
Your problem is that named parameters is optional by default in Dart. And if a parameter does not specify a default value, the default value is going to be null.
This is a problem when using null-safety since your variables are declared to never be allowed to have the value null. But by not requiring values for the parameters, there are a risk that some or all of these variables are going to have null as value.
The fix is to use the keyword required to specify that you want a named parameter to be required to get a value when calling the constructor/method. So change your code to:
class Destination {
String imageUrl;
String city;
String country;
String description;
List<Activity> activities;
Destination({
required this.imageUrl,
required this.city,
required this.country,
required this.description,
required this.activities,
});
}
CodePudding user response:
Change this:
Destination({
this.imageUrl ?? "",
this.city ?? "",
this.country ?? "",
this.description ?? "",
this.activities ?? "",
});
now it will work
