NoSuchMethodError: Class '_Type' has no instance getter 'imgPath'

NoSuchMethodError: Class '_Type' has no instance getter 'imgPath'

我正在尝试访问 imgPath 中的字段 class BusinessCard 这是代码

class BusinessCard extends StatelessWidget {


 final String imgPath;
  final String bsnName;
  final String bsnDescription;
  final String bsnLocation;
  final String bsnReview;
  get _imgPath => imgPath;
  BusinessCard(
      this.imgPath, this.bsnName, this.bsnDescription, this.bsnLocation, this.bsnReview);
...}

这是我试图从哪里访问它

class _DetailsTopPartState extends State<DetailsTopPart> {


    Color fcl = const Color(0xffff005d);
      Color lcl = const Color(0xffeb0ec6);
      Color txt = const Color(0xff042fc9);

  dynamic card = BusinessCard;

  decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(card._imgPath)
          ),}`

您在声明变量 card 时似乎没有向 BusinessCard 构造函数提供任何参数。你还把你的 getter 命名为 _imgPath。在 Flutter 中,如果你把 _ 放在某个东西前面,这意味着它是私有的,所以你无法从它的 class.

外部访问它

您可以通过像这样更改代码来解决此问题

dynamic card = BusinessCard;

dynamic card = BusinessCard();

事实上,您试图在 BusinessCard 中获取 imagePath,这是一种类型而不是值。您需要在访问实例变量之前创建此 class 的实例(通过使用 ExampleClass() 调用默认构造函数或通过调用静态构造函数,如 ExampleClass.fromTest(test))。

你可以了解更多Here