1

I can't define this constructor :

class Artists_Showroom extends StatefulWidget {
  Artists_Showroom( //error 1 here
      {Key key,
      @required this.lista,
      @required this.database,
      @required this.artistaCorrente,
      [this.color = null]}). //error 2 here on first square brachet
      : super(key: key);
  final List<Artista> lista;
  final Artista artistaCorrente;
  final Database database;
  final Color color;

Error 1 :

All final variables must be initialized, but 'color' isn't.
Try adding an initializer for the field.

Error 2 :

Expected to find '}'.
Expected an identifier.

It worked until now because I need to add an optional parameter (in this case it's a color). I just never used optional parameters so I don't know how to declare them.

I did some researches and tried different solutions from StackOverflow but no one working.

0

1 Answer 1

2

You can't have Positional optional parameters [] and Named optional parameters {} at the same time. Have a look at this. If you want too keep your Named parameters you can do the following.

class Artists_Showroom extends StatefulWidget {
  const Artists_Showroom({
    Key key,
    @required this.lista,
    @required this.database,
    @required this.artistaCorrente,
    this.color,
  }) : super(key: key);

  final List<Artista> lista;
  final Artista artistaCorrente;
  final Database database;
  final Color color;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok but this time I need to pass "null" value each time I call the constructor right?
No you don't. If you don't pass anything then it is implicity null. Just call it like Artis_Showroom(lista: lista, database:database, artistaCorrente: artistaCorrente) and the color is then null.
Perfect explanation, I don't know why dart docs aren't clear like you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.