最近,在运行Flutter Upgrade后,我发现有关Color类型的错误出现了很多。例如下面的代码,颜色被标记为错误。错误消息是:颜色不是一种类型。
class ChoiceCard extends StatelessWidget {
final String cardText;
final String cardImage;
final double width;
final double height;
final Color color;
final Category category;
final VoidCallback choiceCardCallback;
在尝试找出发生这种情况的原因之后,我确实尝试编译了我的应用程序,并发现该应用程序实际上运行良好,因此没有实际错误。但是,在应用程序中标记了所有这些红色错误后,将很难查看其中是否存在“真实”错误。还有其他人遇到过这个问题。我想清除它。
一如既往的感谢
最佳答案
我知道这甚至还不能永久解决,尽管我建议您让Flutter自己获取变量的类型...
我知道声明变量的类型有很多好处,但是,作为一种临时解决方法,我建议您现在像这样声明一个最终变量:
class ChoiceCard extends StatelessWidget {
final cardText;
final cardImage;
final width;
final height;
final color;
final category;
final choiceCardCallback;
如果需要,请确保在类构造函数中包括final字段:ChoiceCard({this.cardText, this.cardImage, this.width, this.height, this.color, this.category, this.choiceCardCallback});
接下来,在这种情况下为变量分配值color
时:color = Colors.red;
希望这会暂时解决您的错误,因为flutter本身会发现color
的数据类型。谢谢,让我知道是否有帮助。
关于flutter - flutter 如何解决错误消息:颜色不是类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62989731/