Dart、flutter - 参数类型 'String' 无法分配给参数类型 'Color?'

Dart, flutter - The argument type 'String' can't be assigned to the parameter type 'Color?'

我正在尝试从 dart 中的模型文件中将容器颜色分配给 class 属性。但是它给了我一个错误,参数类型 'String' 不能分配给参数类型 'Color?'.

这是我的模型文件

Driver(
  name: 'Max Verstappen',
  rank: '1',
  team: 'Red Bull Racing',
  points: '182',
  color: 'Colors.blue[800]',
)

这是容器小部件

Padding(
 padding: EdgeInsets.only(left: 15),
 child: Container(
   width: 4,
   height: 50,
   color: driver.color,
 ),
),

错误

The argument type 'String' can't be assigned to the parameter type 'Color?

颜色的类型是Color

import 'dart:ui';

class Driver {
  final String name;
  final String rank;
  final String team;
  final String points;
  final Color? color;

  Driver({this.name, this.rank, this.team, this.points, this.color});
}

当你调用它时,记得在你的 dart 文件顶部导入 material 包以获取 Colors class.

import 'package:flutter/material.dart';

final driver = Driver(
  name: 'Max Verstappen',
  rank: '1',
  team: 'Red Bull Racing',
  points: '182',
  color: Colors.blue[800],
);