简体   繁体   English

Dart:为可选参数错误分配默认值

[英]Dart: Assigning default values to optional parameters errors

I have a bunch of variables in my class, but not every one of them is required in every setting.我的 class 中有一堆变量,但并非每个设置都需要其中的每一个。 I want them to be assignable via the constructor, but only optionally.我希望它们可以通过构造函数分配,但只是可选的。

I've run into the problem of assigning default values based the constants I have defined in the very same class. This is all of the relevant code:我遇到了根据我在同一个 class 中定义的常量分配默认值的问题。这是所有相关代码:

class ExerciseSettings {
  // Units available
  static const List<String> repUnits = ["x"];
  static const List<String> timeUnits = ["s", "min", "h"];
  static const List<String> weightUnits = ["kg", "lbs"];
  static const List<String> intensityUnits = ["/10"];

  // Attributes needed to be assigned
  bool useReps = true;
  bool useWeight = true;
  String repUnit = repUnits[0];
  String timeUnit = timeUnits[0];
  String weightUnit = weightUnits[0];
  String intensityUnit = intensityUnits[0];

  ExerciseSettings({
    this.useReps,       // This errors with the "null value" error as described below
    this.useWeight,
    this.repUnit = repUnits[0],  // These two error with "value must be a constant"
    this.timeUnit = ExerciseSettings.repUnits[0],
    this.weightUnit = const repUnits[0],  // And these two error with "... is not a class"
    this.intensityUnit = const ExerciseSettings.repUnits[0]
  });

This errors with "The parameter 'useReps' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required'".此错误与“参数'useReps'由于其类型不能具有'null'值,但隐式默认值为'null'。尝试添加显式非'null'默认值或'required' '”。

I don't understand why this is.我不明白这是为什么。 First of, why is initializing the variables not taken into account?首先,为什么不考虑初始化变量? They have values assigned, meaning they shouldn't be null.它们已分配值,这意味着它们不应该是 null。

Second of, why can I not assign the static constants as default values inside the constructor?其次,为什么我不能在构造函数中将 static 常量指定为默认值?

Yo can't pass a list item to a constant value, so try this:你不能将列表项传递给常量值,所以试试这个:

class ExerciseSettings {
  // Units available
  static const List<String> repUnits = ["x"];
  static const List<String> timeUnits = ["s", "min", "h"];
  static const List<String> weightUnits = ["kg", "lbs"];
  static const List<String> intensityUnits = ["/10"];

  // Attributes needed to be assigned
  final bool? useReps;

  final bool? useWeight;

  final String? repUnit;

  final String? timeUnit;

  final String? weightUnit;

  final String? intensityUnit;

  ExerciseSettings({
    this.useReps = true,
    this.useWeight = true,
    String? repUnit,
    String? timeUnit,
    String? weightUnit,
    String? intensityUnit,
  })  : repUnit = repUnit ?? repUnits[0],
        timeUnit = timeUnit ?? ExerciseSettings.repUnits[0],
        weightUnit = weightUnit ?? repUnits[0],
        intensityUnit = intensityUnit ?? ExerciseSettings.repUnits[0];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可选参数的默认值必须为常数 flutter / dart - Default values of an optional parameter must be constant flutter / dart Dart 默认可空参数 - Dart Default Nullable Parameters Dart / Flutter 子类化以使用可选参数 - Dart / Flutter subclassing to use optional parameters 无法使用可选参数定义构造函数 Dart - Can't define constructor with optional parameters Dart 如何使用 Dart 中的参数定义默认 function? - How to define a default function with parameters in Dart? Dart/Flutter 小部件,带有可选参数,但至少需要一个 - Dart/Flutter widget with optional parameters but at least one required Flutter 2.0 / Dart - 如何创建带有可选参数的构造函数? - Flutter 2.0 / Dart - How to create a constructor with optional parameters? Dart:如何使用“可选链接”或默认值? - Dart: How do I use "optional chaining" or a default value? Dart Flutter:为类构造函数设置默认值时,可选参数的默认值必须是常量 - Dart Flutter: The default value of an optional parameter must be constant when setting a default value to class constructor 如何在 dart 的给定代码中使用可选参数? 当我删除所需的关键字时,它显示错误 - How can I use optional parameters in given code of dart? when I remove the required keyword it shows error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM