mzsdev
mzsdev

Reputation: 320

The named parameter isn't defined:

I've been having problems since the null safety update came. I searched a lot but couldn't find enough resources.

class AllCoins {
  String symbol;
  String iconUrl;
  double marketCap;
  double price;

  AllCoins(this.symbol, this.iconUrl, this.marketCap, this.price);
  factory AllCoins.fromJson(dynamic json) {
    return AllCoins(
      symbol: json["symbol"],
      iconUrl: json["iconUrl"],
      marketCap: json["marketCap"],
      price: json["price"],
    );
  }
}

error: 4 positional argument(s) expected, but 0 found. 
error: The named parameter 'symbol' isn't defined.
error: The named parameter 'iconUrl' isn't defined. 
error: The named parameter 'marketCap' isn't defined. 
error: The named parameter 'price' isn't defined. 

Actually I just wanted to get data with null safety active but there is no helpful video. I'm still trying to learn flutter and dart but it gets harder as updates come.

Upvotes: 1

Views: 4152

Answers (2)

Sweta Jain
Sweta Jain

Reputation: 4324

For null safety use this code:

class AllCoins {
  
  String? symbol;
  String? iconUrl;
  double? marketCap;
  double? price;
  
  AllCoins({this.symbol,this.iconUrl,this.marketCap,this.price});


  factory AllCoins.fromJson(dynamic json) {
    return AllCoins(
      symbol: json["symbol"],
      iconUrl: json["iconUrl"],
      marketCap: json["marketCap"],
      price: json["price"],
    );
  }
}

Upvotes: 0

Rohan Thacker
Rohan Thacker

Reputation: 6347

You get this error because you're using named arguments where positional arguments are expected.

To use named parameters update the constructor by adding { } around the parameters

AllCoins({required this.symbol,required  this.iconUrl,required  this.marketCap, required this.price});

To continue using positional parameters, update fromJson:

factory AllCoins.fromJson(dynamic json) {
    return AllCoins(
      json["symbol"],
      json["iconUrl"],
      json["marketCap"],
      json["price"],
    );
  }

Upvotes: 1

Related Questions