1
Color pickerColor = new Color(0xff38ada9);
String colorString = pickerColor.toString();
Color newColor = Color(pickerColor.value);

But I got this error

Only static members can be accessed in initializers.

String colorString = this.pickerColor.toString();
Color newColor = Color(this.pickerColor.value);

I tried this and I got this error

Invalid reference to 'this' expression

Any suggestions ?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
joseph_hrs
  • 13
  • 4
  • Possible duplicate of [How to convert Flutter color to string and back to a color](https://stackoverflow.com/questions/49835146/how-to-convert-flutter-color-to-string-and-back-to-a-color) – mirkancal Sep 04 '19 at 14:10

2 Answers2

1

Replace

Color pickerColor = new Color(0xff38ada9);

with

static Color pickerColor = new Color(0xff38ada9);

And you won't have any error in

String colorString = pickerColor.toString();
Color newColor = Color(pickerColor.value);
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
0

Only static members can be accessed in initializers.

Never initialize a class variable during it's declaration unless it is static. But making the variable static will create a common copy for all the objects/Widgets you create with it. Also, it will be easily accessible outside the class.

Note : This error has no relation with your question.

In order to convert Color to String you'll just need to use toString() getter function on the color in which you want to convert to String.

Sample Code:

Color(0xffffffff).toString() // This is what you were looking for.
Mohit Shetty
  • 1,551
  • 8
  • 26