You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart新手求助:解决‘预期3个位置参数但实际传入0个’报错及Tile类实例化问题

Fixing the "3 positional argument(s) expected, but 0 found" Error in Dart

Hey there! Let's figure out how to fix this error you're hitting. The message is straightforward: your Tile class's constructor requires 3 positional arguments, but you're trying to create a Tile instance without passing any of them—hence the "0 found" part.

First, let's start by checking your Tile class definition. It probably looks something like this, with a constructor that expects 3 required positional parameters:

class Tile {
  final String imageAssetPath;
  final bool selected;
  final String thirdRequiredParam; // This is the missing parameter you overlooked

  // Constructor that takes 3 positional arguments in order
  Tile(this.imageAssetPath, this.selected, this.thirdRequiredParam);
}

When you use List.generate to create Tile instances, you need to pass all three arguments in the exact order the constructor defines. For example, if your parent component has a selected variable you want to reuse, and you have image paths ready, your code should look like this:

// Example values from your parent component or logic
bool parentSelected = false;
List<String> imageAssets = ['assets/tile1.png', 'assets/tile2.png', 'assets/tile3.png'];

List<Tile> tileList = List.generate(
  imageAssets.length,
  (index) => Tile(
    imageAssets[index], // 1st argument: imageAssetPath
    parentSelected,    // 2nd argument: selected (from parent)
    'tile_$index',     // 3rd argument: the missing required parameter
  ),
);

Quick Troubleshooting Checks:

  • Double-check your Tile constructor: Confirm exactly what three parameters it needs (their names and data types).
  • Match the order: Positional arguments must be passed in the same sequence they're defined in the constructor.
  • Validate values: Make sure each argument you pass is valid (e.g., image path is a non-null string, selected is a boolean, etc.).

If you had intended some parameters to be optional, you'd wrap them in square brackets [] in the constructor—but the error message clearly states 3 positional arguments are required, so they're definitely mandatory.

Give this adjustment a try, and your Tile instances should be created without that frustrating error!

内容的提问来源于stack exchange,提问作者Elhadji NDIONGUE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:57:29