T
T
trickster20192022-01-27 11:28:00
Dart
trickster2019, 2022-01-27 11:28:00

How to change data type in Datr?

Good afternoon, I started taking a course in the Dart language and met with the following problem.
Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
bin/class.dart:16
this.fuel Capacity = double.parse(map["fuel Capacity"]);
I kind of changed the data type, but it still throws an error.

class Vehicle {
  var manufacturer;
  var fuelCapacity;
  var fuelRemaining;

  String showInfo() =>
      "$manufacturer: $fuelRemaining fo $fuelCapacity (critical: $criticalFuelLevel)";

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

  Vehicle({this.manufacturer, this.fuelCapacity, this.fuelRemaining});

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

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

  vehicle.newFuelRemaining = 20;

  var vehicle2 = Vehicle.fromMap(
      {"fuelRemaining": "dsd", "fuelCapacity": "50", "fuelRemaining": "32"});
  print(vehicle2.showInfo());

  print(vehicle.showInfo());
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2022-01-27
@trickster2019

because there is no guarantee that the map object has a value for the given keys. you need to specify with ?? what value will be the default
next
var manufacturer;
var fuelCapacity;
var fuelRemaining;
you never need to write like that, explicitly indicate the type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question