gpt4 book ai didi

flutter - 不能将 'Color'类型的值分配给 'String'类型的变量

转载 作者:行者123 更新时间:2023-12-03 04:09:29 25 4
gpt4 key购买 nike

使用material.dart库中的Colors.black属性时出现错误。
错误:无法将'Color'类型的值分配给'String'类型的变量。尝试更改变量的类型,或将右侧类型转换为“字符串”。

import 'package:flutter/material.dart';

class Category {
final String id;
final String title;
final String color;

const Category({
@required this.id,
@required this.title,
this.color = Colors.black,
});
}
enter image description here
任何帮助将不胜感激

最佳答案

您尝试将类型Color分配给String类型的变量,请更改:

final String color;
对于:
final Color color;
Dart是一种强类型语言,它禁止您将其他类型的值分配给已声明类型的变量。 ColorString在Dart中都是 Objects,但类型不同。
如果您不确定要传递给变量的类型,请使用关键字 var进行声明:
var color;
这样,您可以为其分配任何类型。不建议您这样做,因为这样一来,您将无法享受使用Dart等强类型语言所带来的好处。

关于flutter - 不能将 'Color'类型的值分配给 'String'类型的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62906896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com