I am new to programming particularly to dart. I have two files called driver_picture_carousel_model.dart and driver_model.dart .

This is the driver_picture_carousel_model.dart file

class PictureCarousel {
  String maxVerstappen;
  String lewisHamilton;
  String sergioPerez;
  String landoNorris;
  String valterriBottas;
  String charlesLeclerc;

  PictureCarousel({
    required this.maxVerstappen,
    required this.lewisHamilton,
    required this.sergioPerez,
    required this.landoNorris,
    required this.valterriBottas,
    required this.charlesLeclerc,
  });
 }

 List<PictureCarousel> pictureCarousels = [
   PictureCarousel(
     maxVerstappen: 'assets/images/MaxVerstappen1.jpg',
     lewisHamilton: 'assets/images/LewisHamilton1.jpg',
     sergioPerez: 'assets/images/SergioPerez1.jpg',
     landoNorris: 'assets/images/LandoNorris1.jpg',
     valterriBottas: 'assets/images/ValtteriBottas1.jpg',
     charlesLeclerc: 'assets/images/CharlesLeclerc1.jpg',
   ),
   PictureCarousel(
     maxVerstappen: 'assets/images/MaxVerstappen2.jpg',
     lewisHamilton: 'assets/images/LewisHamilton2.jpg',
     sergioPerez: 'assets/images/SergioPerez2.jpg',
     landoNorris: 'assets/images/LandoNorris2.jpg',
     valterriBottas: 'assets/images/ValtteriBottas2.jpg',
     charlesLeclerc: 'assets/images/CharlesLeclerc2.jpg',
  ),
];

This is the driver_model.dart file, I have imported the driver_picture_carousel_model.dart file and I tried to assign an object in the Driver class to the pictureCarousels list.

import 'package:formula1_app/models/driver_picture_carousel_model.dart';

class Driver {
   String name;
   String rank;
   String team;
   String points;
   String color;
   String number;
   String carousel;

   Driver({
     required this.name,
     required this.rank,
     required this.team,
     required this.points,
     required this.color,
     required this.number,
     required this.carousel,
   });
 }

 List <Driver> drivers = [
   Driver(
     name: 'Max Verstappen',
     rank: '1',
     team: 'Red Bull Racing',
     points: '182',
     color: 'blue[800]',
     number: '33',
     carousel: pictureCarousels.maxVerstappen,
   ),
  ];

However it gave me this error

The getter 'maxVerstappen' isn't defined for the type 'List<PictureCarousel>'.
Try importing the library that defines 'maxVerstappen', correcting the name to the
name of an existing getter, or defining a getter or field named 'maxVerstappen'.       


Solution 1: Lucas Josino

pictureCarousels is a list of PictureCarousel. Try select which item inside list you want to use, like:

List <Driver> drivers = [
   Driver(
     name: 'Max Verstappen',
     rank: '1',
     team: 'Red Bull Racing',
     points: '182',
     color: 'blue[800]',
     number: '33',
     //pictureCarousels[0].maxVerstappen or pictureCarousels[1].maxVerstappen
     carousel: pictureCarousels[0].maxVerstappen,
   ),
  ];