dart 所有final变量都必须初始化,但'city'不是

kninwzqo  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(126)

这是我的代码

final City city;
CityCard(city, {super.key, dynamic id, String? name, String? imageUrl});

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(18),
      child: Container(
        height: 150,
        width: 120,
        color: Color(0xffF6F7F8),
        child: Column(
          children: [
            Stack(
              children: [
                Image.asset(
                  city.imageUrl,
                  width: 120,
                  height: 102,
                  fit: BoxFit.cover,
                ),
                city.isPopular
                    ? Align(
                        alignment: Alignment.topRight,
                        child: Container(
                          width: 50,
                          height: 30,
                          decoration: BoxDecoration(
                              color: purpleColor,
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(36),
                              )),
                          child: Center(
                            child: Image.asset(
                              'assets/star.png',
                              width: 22,
                              height: 22,
                            ),
                          ),
                        ),
                      )
                    : Container(),
              ],
            ),
            const SizedBox(
              height: 11,
            ),
            Text(
              city.name,
              style: blackTextStyle.copyWith(
                fontSize: 16,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

字符串
我试着去弥补,但还是不能

final City city;
CityCard(dynamic? city, {super.key, dynamic id, String? name, String? imageUrl});

yfjy0ee7

yfjy0ee71#

对于您的情况,您需要使用this.city

CityCard( this.city,..);

字符串

相关问题