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

kninwzqo  于 2024-01-04  发布在  其他
关注(0)|答案(1)|浏览(187)

这是我的代码

  1. final City city;
  2. CityCard(city, {super.key, dynamic id, String? name, String? imageUrl});
  3. @override
  4. Widget build(BuildContext context) {
  5. return ClipRRect(
  6. borderRadius: BorderRadius.circular(18),
  7. child: Container(
  8. height: 150,
  9. width: 120,
  10. color: Color(0xffF6F7F8),
  11. child: Column(
  12. children: [
  13. Stack(
  14. children: [
  15. Image.asset(
  16. city.imageUrl,
  17. width: 120,
  18. height: 102,
  19. fit: BoxFit.cover,
  20. ),
  21. city.isPopular
  22. ? Align(
  23. alignment: Alignment.topRight,
  24. child: Container(
  25. width: 50,
  26. height: 30,
  27. decoration: BoxDecoration(
  28. color: purpleColor,
  29. borderRadius: BorderRadius.only(
  30. bottomLeft: Radius.circular(36),
  31. )),
  32. child: Center(
  33. child: Image.asset(
  34. 'assets/star.png',
  35. width: 22,
  36. height: 22,
  37. ),
  38. ),
  39. ),
  40. )
  41. : Container(),
  42. ],
  43. ),
  44. const SizedBox(
  45. height: 11,
  46. ),
  47. Text(
  48. city.name,
  49. style: blackTextStyle.copyWith(
  50. fontSize: 16,
  51. ),
  52. ),
  53. ],
  54. ),
  55. ),
  56. );
  57. }
  58. }

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

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

yfjy0ee7

yfjy0ee71#

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

  1. CityCard( this.city,..);

字符串

相关问题