0

Quite possible an absurdly easy question but I've done a lot of beginner tutorials and this never seems to be covered, in below, both boardPosition.name and color are items in a data list. I can pass thru the name no problem, but the color doesn't work probably because it's dragging through the apostrophe. However, I can't seem to work out how else to do this...

Text(
    boardPosition.name,
    style: TextStyle(
        fontSize:18.0,
        fontFamily:'Amatic',
        color: boardPosition.color,
        )
    ),

Thanks in advance :)

Here's the board position:

class Board {

  String name;
  final color;
  int state;
  int myScore;
  int compScore;

  Board({this.name, this.color, this.state, this.myScore, this.compScore});
}

List <Board> boardPosition = [
  Board(name : "purple", color: "Colors.purple", state : 0, myScore: 0, compScore: 0),
  Board(name : "red", color: "Colors.red", state : 0, myScore: 0, compScore: 0),
  Board(name : "yellow", color: "Colors.yellow", state : 0, myScore: 0, compScore: 0),
  Board(name : "blue", color: "Colors.blue", state : 0, myScore: 0, compScore: 0),
  Board(name : "orange", color: "Colors.orange", state : 0, myScore: 0, compScore: 0),
  Board(name : "green", color: "Colors.green", state : 0, myScore: 0, compScore: 0),
  Board(name : "pink", color: "Colors.pink", state : 0, myScore: 0, compScore: 0),
];
3
  • Can you share the error message? Commented Feb 6, 2020 at 13:10
  • Can you give us an example of an item from your list Commented Feb 6, 2020 at 13:23
  • Error I get is "type string is not a subtype of type Color" Commented Feb 6, 2020 at 14:05

1 Answer 1

1

You have to set the color field to a Color Object not a String. Try:

List <Board> boardPosition = [
  Board(name : "purple", color: Colors.purple, state : 0, myScore: 0, compScore: 0),
  Board(name : "red", color: Colors.red, state : 0, myScore: 0, compScore: 0),
  Board(name : "yellow", color: Colors.yellow, state : 0, myScore: 0, compScore: 0),
  Board(name : "blue", color: Colors.blue, state : 0, myScore: 0, compScore: 0),
  Board(name : "orange", color: Colors.orange, state : 0, myScore: 0, compScore: 0),
  Board(name : "green", color: Colors.green, state : 0, myScore: 0, compScore: 0),
  Board(name : "pink", color: Colors.pink, state : 0, myScore: 0, compScore: 0),
];
Sign up to request clarification or add additional context in comments.

1 Comment

You should define color's type explicitly: Color color;

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.