【问题标题】:error: Too many positional arguments: 0 expected, but 3 found错误:位置参数太多:预期为 0,但找到了 3
【发布时间】:2019-08-30 20:22:40
【问题描述】:

我收到此错误:位置参数过多:预期为 0,但找到了 3。 (extra_positional_arguments_could_be_named at itemBuilder...无法弄清楚是什么原因造成的。

错误出现在这里 ->

itemBuilder: (ctx, i) => ProductItem(
          loadedProducts[i].id,
          loadedProducts[i].title,
          loadedProducts[i].imageUrl,
        ),
class ProductListPage extends StatelessWidget {
  ProductListPage({this.context});

  final List<Product> loadedProducts = [
    Product(
      id: 'p1',
      title: "Michael Kora",
      description: 'this is cool',
      price: 699,
      imageUrl:
          "https://n1.sdlcdn.com/imgs/c/9/8/Lambency-Brown-Solid-Casual-Blazers-SDL781227769-1-1b660.jpg",
    ),
    Product(
      id: 'p1',
      title: "Michael Kora",
      description: 'this is cool',
      price: 699,
      imageUrl:
          "https://n1.sdlcdn.com/imgs/c/9/8/Lambency-Brown-Solid-Casual-Blazers-SDL781227769-1-1b660.jpg",
    ),

  ];

  final BuildContext context;

//  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: GridView.builder(
        padding: const EdgeInsets.all(10.0),
        itemCount: loadedProducts.length,
        itemBuilder: (ctx, i) => ProductItem(
          loadedProducts[i].id,
          loadedProducts[i].title,
          loadedProducts[i].imageUrl,
        ),

    );
  }
}

ProductItem定义如下:

class ProductItem extends StatelessWidget { 
  ProductItem({this.id, this.imageUrl, this.title}); 

  final String id;
  final String title;
  final String imageUrl;

  @override
  Widget build(BuildContext context) {
    return GridTile(
      child: Image.network(imageUrl),
    );
  } 
}

【问题讨论】:

  • 你能告诉我们ProductItem构造函数的签名吗?
  • 类 ProductItem 扩展 StatelessWidget { ProductItem({this.id, this.imageUrl, this.title});最终字符串 id;最终字符串标题;最终字符串 imageUrl; @override Widget build(BuildContext context) { return GridTile( child: Image.network(imageUrl), ); } }

标签: flutter dart


【解决方案1】:

简答:

ProductItem 需要 named,而不是 positional 参数。像这样初始化它:

ProductItem(
  id: loadedProducts[i].id,
  title: loadedProducts[i].title,
  imageUrl: loadedProducts[i].imageUrl,
)

更长的答案:

错误提示我们:

位置参数过多:预期为 0,但找到了 3 个。

这表明ProductItem 构造函数期望位置参数。它不知道如何处理它们。为什么?让我们检查ProductItem 类定义:

class ProductItem extends StatelessWidget { 
  ProductItem({this.id, this.imageUrl, this.title}); 
  ...
}

参数包含在{}中。在 Dart 中,这意味着它们是可选的 named 参数。也就是说,如果您决定传递它们,则必须像这样传递它们:

ProductItem(id: 'id', imageUrl: 'url', title: 'title')

请注意,每个参数都以它的名称开头 - 因此它被称为 named 参数。相比之下,positional 参数的区别仅在于它们在调用构造函数时所处的位置。

类定义告诉我们ProductItem 的构造函数不应使用位置 参数调用。相反,应该使用 named 参数。 ProductItem 应该这样构造:

ProductItem(
  id: loadedProducts[i].id,
  title: loadedProducts[i].title,
  imageUrl: loadedProducts[i].imageUrl,
)

要了解有关参数类型的更多信息,请参阅 Dart documentation。或者,探索DartPad 中的差异。

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 2021-09-21
    • 2022-07-20
    • 2021-11-09
    • 2020-04-20
    • 2021-11-10
    • 2021-06-14
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多