我怎样才能满足“(int) => void”不是类型“((num) => void)”的子类型?例外?

How can I satisfy '(int) => void' is not a subtype of type '((num) => void)?' exception?

我在我的 Flutter 项目中使用 pref 插件。我有两个用于配置下限和上限的滑块设置。我想防止用户在下限大于上限或相反时配置无效的组合设置。

不幸的是,PrefSlider 没有验证方法,因此我试图在事后在 onChanged 处理程序中灭火:

      PrefSlider<int>(
        title: "Lower Limit",
        pref: "lower_limit",
        trailing: (num value) => Text("$value"),
        min: 0,
        max: 300,
        onChange: (value) {
          final upperLimit = PrefService.of(context).get<int>("upper_limit") ?? 300;
          if (value >= upperLimit) {
            PrefService.of(context).set<int>("lower_limit", upperLimit - 1);
          }
        },
      ),
      PrefSlider<int>(
        title: "Upper Limit",
        pref: "upper_limit",
        trailing: (num value) => Text("$value"),
        min: 0,
        max: 300,
        onChange: (value) {
          final lowerLimit = PrefService.of(context).get<int>("lower_limit") ?? 0;
          if (value <= lowerLimit) {
            PrefService.of(context).set<int>("upper_limit", lowerLimit + 1);
          }
        },
      ),

当我移动滑块并且 pref 尝试调用我的 onChanged 函数时,我得到一个大异常:

E/flutter (21583): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type '(int) => void' is not a subtype of type '((num) => void)?'
E/flutter (21583): #0      _PrefSliderState._onChange (package:pref/src/slider.dart:90:18)
E/flutter (21583): #1      _SliderState._handleChanged (package:flutter/src/material/slider.dart:540:24)
E/flutter (21583): #2      _RenderSlider._startInteraction (package:flutter/src/material/slider.dart:1235:17)
E/flutter (21583): #3      _RenderSlider._handleTapDown (package:flutter/src/material/slider.dart:1296:5)
E/flutter (21583): #4      TapGestureRecognizer.handleTapDown.<anonymous closure> (package:flutter/src/gestures/tap.dart:580:61)
E/flutter (21583): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (21583): #6      TapGestureRecognizer.handleTapDown (package:flutter/src/gestures/tap.dart:580:11)
E/flutter (21583): #7      BaseTapGestureRecognizer._checkDown (package:flutter/src/gestures/tap.dart:287:5)
E/flutter (21583): #8      BaseTapGestureRecognizer.didExceedDeadline (package:flutter/src/gestures/tap.dart:258:5)
E/flutter (21583): #9      PrimaryPointerGestureRecognizer.didExceedDeadlineWithEvent (package:flutter/src/gestures/recognizer.dart:501:5)
E/flutter (21583): #10     PrimaryPointerGestureRecognizer.addAllowedPointer.<anonymous closure> (package:flutter/src/gestures/recognizer.dart:454:41)
E/flutter (21583): #11     _rootRun (dart:async/zone.dart:1346:47)
E/flutter (21583): #12     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (21583): #13     _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
E/flutter (21583): #14     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1202:23)
E/flutter (21583): #15     _rootRun (dart:async/zone.dart:1354:13)
E/flutter (21583): #16     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (21583): #17     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1186:23)
E/flutter (21583): #18     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (21583): #19     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter (21583): #20     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter (21583): #21     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

我就是想不通哪里出了问题。不知何故处理函数应该可以为空? '((num) => void)?' 中的问号是什么?这是插件尝试调用我的处理程序的代码部分:

  Future<void> _onChange(double value) async {
    final service = PrefService.of(context, listen: false);

    if (T == double) {
      service.set<double>(widget.pref, value);

      if (widget.onChange != null) {
        widget.onChange!(value);
      }
    } else if (T == int) { // for PrefSlider<int>
      service.set<int>(widget.pref, value.round());

      if (widget.onChange != null) {
        widget.onChange!(value.round()); // <- this is the call
      }
    } else if (T == num) {
      service.set<double>(widget.pref, value);

      if (widget.onChange != null) {
        widget.onChange!(value);
      }
    }
  }

从更广阔的角度来看,我在想也许我可以将 minmax 字段转换为状态,并以某种方式在每次更改时重建,但这可能会破坏 UI.

将通用类型更改为 num 而不是 int

PrefSlider<num>

看来我可以通过为回调指定显式 num 类型来缓解异常:

  PrefSlider<int>(
    title: "Lower Limit",
    pref: "lower_limit",
    trailing: (num value) => Text("$value"),
    min: 0,
    max: 300,
    onChange: (num value) {
      final upperLimit = PrefService.of(context).get<int>("upper_limit") ?? 300;
      if (value >= upperLimit) {
        PrefService.of(context).set<int>("lower_limit", upperLimit - 1);
      }
    },
  ),
  PrefSlider<int>(
    title: "Upper Limit",
    pref: "upper_limit",
    trailing: (num value) => Text("$value"),
    min: 0,
    max: 300,
    onChange: (num value) {
      final lowerLimit = PrefService.of(context).get<int>("lower_limit") ?? 0;
      if (value <= lowerLimit) {
        PrefService.of(context).set<int>("upper_limit", lowerLimit + 1);
      }
    },
  ),

有趣的是,这些都是 运行 时的例外情况,如果我从 trailing: (num value) => Text("$value"),.

中删除明确的 num 也会发生这种情况