Propriété calculée d'une clase à un widget - Getter

Bonjour à tous :slight_smile:

J’essai de faire une sorte de « mise en forme conditionnelle » d’un widget « View » depuis la classe qui l’alimente mais sans succès :

J’ai un objet provenant d’une classe Player avec une propriété calculée de couleur teamColor :

class Player {
  final String pseudo;
  final String team;

  Player({
    this.pseudo = "-",
    this.team="Red",
  });

  Color get teamColor {
    if (team == "Red") {
      return Color.fromARGB(255, 255, 0, 0);
    } else {
      return Color.fromARGB(255, 0, 0, 255);
    }
  }
}

Quand je crée mon stateful widget pour colorier la bordure du container de la couleur de l’équipe en question, je souhaite utiliser une variable _borderColorTile que je pourrai réutiliser plusieurs fois dans mon code. Mais le widget.player.teamColor est souligné en rouge (le widget) :
image

class PlayerTile extends StatefulWidget {
  final Player player;

  const PlayerTile({
    super.key,
    required this.player,
  });

  @override
  State<PlayerTile> createState() => _PlayerTileState();
}

class _PlayerTileState extends State<PlayerTile> {

  //Color variable
  final Color _borderColorTile = widget.player.teamColor;

Savez vous pourquoi ceci ne fonctionne pas ?

Merci d’avance.

Hello @Xababa_Dalabama,

Tu pourrais nous mettre le message d’erreur ?

Hello @Tazooou

Quand je mets le

widget.player.teamColor 

directement dans l’écriture des widget ca fonctionne. Je pensais utile d’utiliser une variable intermédiaire qui m’aurait permis de changer d’avis plus tard sans le changer dans toutes les lignes du code.

Voilà le message d’erreur

The current configuration.

A [State] object's configuration is the corresponding [StatefulWidget] instance. This property is initialized by the framework before calling [initState]. If the parent updates this location in the tree to a new widget with the same [runtimeType] and [Widget.key] as the current configuration, the framework will update this property to refer to the new widget and then call [didUpdateWidget], passing the old configuration as an argument.

The instance member 'widget' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expressiondartimplicit_this_reference_in_initializer