【问题标题】:Dart internal constructorDart 内部构造函数
【发布时间】:2017-07-27 19:31:14
【问题描述】:

我在执行 Widget 的构造函数时遇到奇怪的运行时错误。

这是课程:

class NumberPicker extends StatefulWidget {

  NumberPicker.integer({
    Key key,
    @required int initialValue,
    @required int minValue,
    @required int maxValue,
  })
      : assert(initialValue != null),
        assert(minValue != null),
        assert(maxValue != null),
        assert(maxValue > minValue),
        assert(initialValue >= minValue &&
            initialValue <= maxValue),
        this._internal(
          minDoubleValue: -1.0,
          maxDoubleValue: -1.0,
          initialDoubleValue: -1.0,
          decimalPlaces: 0,
          minIntValue: minValue,
          maxIntValue: maxValue,
          initialIntValue: initialValue
      );


  NumberPicker._internal({
    Key key,
    @required this.minDoubleValue,
    @required this.maxDoubleValue,
    @required this.initialDoubleValue,
    @required this.decimalPlaces,
    @required this.minIntValue,
    @required this.maxIntValue,
    @required this.initialIntValue,
  }) :
        super(key: key);


  final double minDoubleValue;
  final double maxDoubleValue;
  final double initialDoubleValue;
  final int decimalPlaces;
  final int minIntValue;
  final int maxIntValue;
  final int initialIntValue;

  @override
  _NumberPickerState createState() => new _NumberPickerState();
}

当我打电话给new NumberPicker.integer(initialValue: 50, minValue: 1, maxValue: 100)

我收到以下错误:

I/flutter ( 3873): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter ( 3873): The following _CompileTimeError was thrown while handling a gesture:
I/flutter ( 3873): 'package:numberpicker/numberpicker.dart': error: line 20: '=' expected
I/flutter ( 3873):   this._internal(
I/flutter ( 3873):                 ^

值得一提的是代码可以编译,我没有语法错误,但是当我调用构造函数时,我只看到堆栈跟踪:(

谁能向我解释我做错了什么?

【问题讨论】:

  • 如果你删除断言它应该工作

标签: dart flutter


【解决方案1】:

如果你想调用你的类的另一个构造函数,你应该在NumberPicker.integer上使用工厂构造函数,但我不知道为什么你没有从分析器中得到任何错误,也许你应该填写一个错误.

factory NumberPicker.integer({
  Key key,
  @required int initialValue,
  @required int minValue,
  @required int maxValue,
}) {
  assert(initialValue != null);
  assert(minValue != null);
  assert(maxValue != null);
  assert(maxValue > minValue);
  assert(initialValue >= minValue && initialValue <= maxValue);

  return new NumberPicker._internal(
      minDoubleValue: -1.0,
      maxDoubleValue: -1.0,
      initialDoubleValue: -1.0,
      decimalPlaces: 0,
      minIntValue: minValue,
      maxIntValue: maxValue,
      initialIntValue: initialValue
  );
}

【讨论】:

    【解决方案2】:

    你的初始化列表中不能有断言,只有字段赋值。 https://dartpad.dartlang.org/71eab37e91c7d94e2dbcf7817def32d5 工作正常。

    【讨论】:

    • 是的,你可以通过添加 enableAssertInitializer: true 到 analysis_options.yaml
    【解决方案3】:

    这是不可访问的,而不是 this._internal 使用 NumberPicker._internal,'new' 关键字在颤振中不再需要。

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 2016-03-11
      • 2020-05-05
      • 1970-01-01
      相关资源
      最近更新 更多