[英]How to convert color to string in Flutter?
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
对“this”表达式的引用无效
Any suggestions ?有什么建议 ?
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);
Only static members can be accessed in initializers.
在初始值设定项中只能访问静态成员。
Never initialize a class variable during it's declaration unless it is static
.永远不要在声明期间初始化类变量,除非它是
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.为了将颜色转换为字符串,您只需要在要转换为字符串的颜色上使用
toString()
getter 函数。
Sample Code:示例代码:
Color(0xffffffff).toString() // This is what you were looking for.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.