CalmTaurus
CalmTaurus

Reputation: 25

The argument type 'List<String>' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable) on the Dart

A value of type 'String?' can't be assigned to a variable of type 'String'. Try changing the type of the variable, or casting the right-hand type to 'String'.dart(invalid_assignment)

  late String manufacturer;
  late double fuelCapacity;
  late double fuelRemaining;

  String showInfo() =>
      '$manufacturer: $fuelRemaining of $fuelCapacity (critical: $criticalFuelLevel)';

  double get criticalFuelLevel => fuelCapacity * 0.1;
  set newFuelRemaining(double val) => fuelRemaining = val;

  // default constructor
  Vehicle(
      {required this.manufacturer,
      required this.fuelCapacity,
      required this.fuelRemaining});

  // named constructor
  Vehicle.fromMap(Map<String,String> map) {
    this.manufacturer = map["manufacturer"];
    this.fuelCapacity = double.parse(["fuelCapacity"]);
    this.fuelRemaining = double.parse(['fuelRemaining']);
  }
}

void main() {
  var vehicle =
      Vehicle(manufacturer: 'BMW', fuelCapacity: 55, fuelRemaining: 20);

  vehicle.newFuelRemaining = 20;

  var vehicle2 = Vehicle.fromMap(
      {'manufacturer': 'KIA', 'fuelCapacity': '50', 'fuelRemaining': '20'});

  print(vehicle2.showInfo());
}```

Upvotes: 1

Views: 169

Answers (1)

Yair Chen
Yair Chen

Reputation: 963

Its because map["manufacturer"]; can't be sure manufacturer is for sure inside the map. If its for sure there you can tell Dart that it cant be null like that (a ! at the end):

 this.manufacturer = map["manufacturer"]!;

Tell me if you need anything else or if the problem is not there :)

Upvotes: 1

Related Questions