Home > Mobile >  error: Too many positional arguments: 0 expected, but 9 found
error: Too many positional arguments: 0 expected, but 9 found

Time:01-12

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

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

enter image description here

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();
    

CodePudding user response:

You used named parameter but I think you need to use parameters when covert your map to list

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();

    

CodePudding user response:

You used named parameters, so you need to name it when declaring the 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).

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();
  •  Tags:  
  • Related