0
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,
  ),
Spatz
  • 18,640
  • 7
  • 62
  • 66

2 Answers2

0

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,
  });
}
julemand101
  • 28,470
  • 5
  • 52
  • 48
0

Change this:

 Destination({
        this.imageUrl ?? "",
        this.city ?? "",
        this.country ?? "",
        this.description ?? "",
        this.activities ?? "",
      });

now it will work