如何解决所有最终变量都必须初始化,但'city'不是

问题描述 投票:0回答:1

这是我的代码

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});
flutter dart constructor
1个回答
0
投票

对于您的情况,您需要使用

this.city

CityCard( this.city,..);
© www.soinside.com 2019 - 2024. All rights reserved.