【问题标题】:The argument type 'Object?' can't be assigned to the parameter type 'Color'参数类型“对象?”不能分配给参数类型“颜色”
【发布时间】:2021-08-18 01:05:46
【问题描述】:

我下面有如下统计数据

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,
                    ),
                  );
                }),

当我调用数据时出现错误, 参数类型“对象?”不能分配给参数类型“颜色”。 怎么会这样?

【问题讨论】:

    标签: flutter dart listview


    【解决方案1】:

    我不确定,但你可以像这样使用item['bgColor'] as Color

    List<CardModel> cards = cardData
        .map((item) => CardModel(
              item['name'] as String,
              item['cardBackground'] as String,
              item['bgColor'] as Color,
            ))
        .toList();
    

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 2023-04-09
      • 2021-12-14
      • 2020-11-29
      • 2021-08-27
      • 2021-06-05
      • 2022-01-16
      • 2021-11-06
      • 2021-08-05
      相关资源
      最近更新 更多