【问题标题】:The argument type 'List<String>' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable) on the Dart参数类型 'List<String>' 不能分配给 Dart 上的参数类型 'String'.dart(argument_type_not_assignable)
【发布时间】:2021-06-06 11:31:48
【问题描述】:

“字符串?”类型的值不能分配给“字符串”类型的变量。 尝试更改变量的类型,或将右侧类型转换为 '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());
}```

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这是因为map["manufacturer"]; 无法确定制造商是否在地图内。如果它确定在那里,你可以告诉 Dart 它不能像那样为空(最后是 !):

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

    如果您还需要什么或者问题不存在,请告诉我:)

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 2021-09-25
      • 2020-04-15
      • 2023-04-05
      • 2021-09-15
      • 1970-01-01
      • 2021-10-20
      • 2021-10-06
      • 1970-01-01
      相关资源
      最近更新 更多