简体   繁体   English

错误:位置 arguments 太多:预期为 0,但找到了 9

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

can some please point out why i am having this issue and how do i fix it?有人可以指出我为什么会遇到这个问题以及如何解决它?

in the terminal i get this message:在终端我收到这条消息:

i really do not know why i am getting this error: i have used the "?"我真的不知道为什么会收到此错误:我使用了“?” null safety symbols but still i keep pointing out the same error null 安全符号,但我仍然指出同样的错误

error: Too many positional arguments: 0 expected, but 9 found. (extra_positional_arguments_could_be_named at [bankingapp] lib\model\card_model.dart:29)

在此处输入图像描述

import 'package:flutter/material.dart';
import 'package:bankingapp/constants/color_constant.dart';

class CardModel {
  String? name;
  String? type;
  String? balance;
  String? valid;
  String? moreIcon;
  String? cardBackground;
  Color? bgColor;
  Color? firstColor;
  Color? secondColor;

  CardModel(
      {this.name,
      this.type,
      this.balance,
      this.valid,
      this.moreIcon,
      this.cardBackground,
      this.bgColor,
      this.firstColor,
      this.secondColor});
}

List<CardModel> cards = cardData
    .map((item) => CardModel(
        item['name'],  // <-- Line 29: error occurs here
        item['type'],
        item['balance'],
        item['valid'],
        item['moreIcon'],
        item['cardBackground'],
        item['bgColor'],
        item['firstColor'],
        item['secondColor']))
    .toList();
    

You used named parameter but I think you need to use parameters when covert your map to list您使用了命名参数,但我认为您需要在将 map 隐藏到列表时使用参数

class CardModel {
  String? name;
  String? type;
  String? balance;
  String? valid;
  String? moreIcon;
  String? cardBackground;
  Color? bgColor;
  Color? firstColor;
  Color? secondColor;

  CardModel(
      {this.name,
      this.type,
      this.balance,
      this.valid,
      this.moreIcon,
      this.cardBackground,
      this.bgColor,
      this.firstColor,
      this.secondColor});
}

List<CardModel> cards = cardData
    .map((item) => CardModel( // here need to change
       name: item['name'], 
        type: item['type'],
        balance: item['balance'],
        valid: item['valid'],
       moreIcon: item['moreIcon'],
        cardBackground:item['cardBackground'],
       bgColor: item['bgColor'],
        firstColor:item['firstColor'],
        secondColor:item['secondColor']))
    .toList();

    

You used named parameters, so you need to name it when declaring the class.您使用了命名参数,因此在声明 class 时需要对其进行命名。 Named parameters are not required, so you dont NEED to use them.命名参数不是必需的,因此您不需要使用它们。 That's why the error (0 parameters expected, as none of them were required).这就是错误的原因(预期为 0 个参数,因为它们都不是必需的)。

So the resolution should be: Remove the {} from the constructor, making the parameter obligatory or use the code:所以解决方案应该是:从构造函数中删除 {},使参数成为强制性参数或使用代码:

class CardModel {
  String? name;
  String? type;
  String? balance;
  String? valid;
  String? moreIcon;
  String? cardBackground;
  Color? bgColor;
  Color? firstColor;
  Color? secondColor;

  CardModel(
      {this.name,
      this.type,
      this.balance,
      this.valid,
      this.moreIcon,
      this.cardBackground,
      this.bgColor,
      this.firstColor,
      this.secondColor});
}

List<CardModel> cards = cardData
    .map((item) => CardModel(
        name: item['name'],  // <-- Line 29: error occurs here
        type: item['type'],
        balance: item['balance'],
        valid: item['valid'],
        moreIcon: item['moreIcon'],
        cardBackground: item['cardBackground'],
        bgColor: item['bgColor'],
        firstColor: item['firstColor'],
        secondColor: item['secondColor']))
    .toList();

Parameter can be pass via two type:参数可以通过两种类型传递:

  1. Positioned Parameter定位参数
  2. Optional Parameter in your code you passed an Optional Parameter.您的代码中的可选参数传递了一个可选参数。 So to fix the code you need to remove "{}" from your CardMModel.因此,要修复代码,您需要从 CardMModel 中删除“{}”。

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

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