我有以下统计数据
import 'package:flutter/material.dart';
import 'package:tesflutter/constants/color_constant.dart';
class CardModel {
String name;
String cardBackground;
Color bgColor;
CardModel(this.name,this.cardBackground, this.bgColor);
}
List<CardModel> cards = cardData
.map((item) => CardModel(
item['name'],
item['cardBackground'],
item['bgColor']))
.toList();
var cardData = [
{
"name": "Prambors",
"cardBackground": 'assets/icons/mastercard_bg.svg',
"bgColor": kMasterCardColor
},
]
还有这个
child: ListView.builder(
padding: EdgeInsets.only(left: 16, right: 8),
scrollDirection: Axis.horizontal,
itemCount: cards.length,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.only(right: 8),
height: 175,
width: 220,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: cards[index].bgColor,
),
);
}),
当我调用数据时出现错误, 参数类型“对象?”不能分配给参数类型“颜色”。 怎么会这样?
答案 0 :(得分:2)
我不确定,但您可以像这样使用 item['bgColor'] as Color
List<CardModel> cards = cardData
.map((item) => CardModel(
item['name'] as String,
item['cardBackground'] as String,
item['bgColor'] as Color,
))
.toList();