简体   繁体   English

错误:位置参数过多:预期为0,但发现3

[英]error: Too many positional arguments: 0 expected, but 3 found

I am getting this error: Too many positional arguments: 0 expected, but 3 found. 我收到此错误:位置参数过多:预期为0,但找到3。 (extra_positional_arguments_could_be_named at itemBuilder...and can not figure out what could be causing it. (extra_positional_arguments_could_be_name在itemBuilder ...并且无法弄清楚是什么原因引起的。

the error is appearing here -> 错误出现在这里->

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 is defined as follows: 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),
    );
  } 
}

Short answer: 简短答案:

ProductItem expects named , not positional arguments. ProductItem希望使用named而不是位置参数。 Initialize it like so: 像这样初始化它:

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

Longer answer: 更长的答案:

The error informs us: 该错误通知我们:

Too many positional arguments: 0 expected, but 3 found. 位置参数过多:预计为0,但找到3。

Which suggests that ProductItem constructor did not expect positional arguments. 这表明ProductItem构造函数希望使用位置参数。 It does not know what to do with them. 它不知道该如何处理他们。 Why? 为什么? Let's inspect ProductItem class definition: 让我们检查一下ProductItem类的定义:

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

The parameters are enclosed in {} . 参数包含在{} In Dart, this means that they are optional named parameters. 在Dart中,这意味着它们是可选的命名参数。 That is, if you decide to pass them in, you must pass them in like so: 也就是说,如果您决定将它们传递,则必须像这样传递它们:

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

Note that each argument is preceded by its name - hence it is called a named parameter. 请注意,每个参数都以其名称开头-因此,它被称为命名参数。 In contrast, positional arguments are differentiated only by the position they take in a call to a constructor. 相反, 位置参数仅通过它们在调用构造函数时所处的位置来区分。

Class definition informs us that constructor of ProductItem should not be called with positional arguments. 类定义告诉我们,不应使用位置参数来调用ProductItem构造函数。 Instead, named arguments should be used. 而是应使用命名参数。 ProductItem should be constructed this way: ProductItem应该以这种方式构造:

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

To read more about types of parameters, refer to Dart documentation . 要了解有关参数类型的更多信息,请参阅Dart 文档 Alternatively, explore the differences in DartPad . 或者,探索DartPad中的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM