Flutter 中的下拉按钮用法 - 未处理的异常:类型 'double' 不是 'value' 类型 'int' 的子类型

Dropdown Button Usage in Flutter - Unhandled Exception: type 'double' is not a subtype of type 'int' of 'value'

我正在尝试使用下拉按钮更改一些值,然后使用这些值更改对象属性并调用对象方法,但是当我尝试这样做时出现以下错误,

num glute_mev = 2;
num glute_mv = 2;
num glute_mrv_2x = 12;
num glute_mrv_3x = 18;
num glute_mrv_4x = 25;

class Mesocycle {

  late num duration;
  late String mode;
  late num freq;

// would prefer to not initialize but it gives an error if i dont
  late num gluteFreq;
  late String gluteMode = 'MAX'; 
  late num gluteIncrement = 1.0;

  List gluteExercises = <String>[];
  List gluteVolume = <int>[];

  generateGluteVolume() {
    //4 week mesocycle
    if (duration == 4 && gluteMode != 'off') {
      gluteVolume.add(glute_mev);
      gluteVolume.add(glute_mev + gluteIncrement * 2);
      gluteVolume.add(glute_mev + gluteIncrement * 3);
      gluteVolume.add(glute_mev + (gluteIncrement * 3) / 3); //deload
    }
    //8 week mesocycle
    if (duration == 8 && gluteMode != 'off') {
      gluteVolume.add(glute_mev);
      gluteVolume.add(glute_mev + gluteIncrement * 2);
      gluteVolume.add(glute_mev + gluteIncrement * 3);
      gluteVolume.add(glute_mev + gluteIncrement * 4);
      gluteVolume.add(glute_mev + gluteIncrement * 5);
      gluteVolume.add(glute_mev + gluteIncrement * 6);
      gluteVolume.add(glute_mev + gluteIncrement * 7);
      gluteVolume.add(glute_mev + (gluteIncrement * 7) / 3); //deload
    }
  }

  set_Freq(String group, num _freq) {
    if (group == 'glutes') {
      gluteFreq = _freq;
    }
  }

  set_Mode(String group, String _mode) {
    if (group == 'glutes') {
      gluteMode = _mode;
    }
  }

  set_gluteIncrement() {
    if (gluteFreq == 2) {
      gluteIncrement = (glute_mrv_2x - glute_mev) / (duration - 1);
    }
    if (gluteFreq == 3) {
      gluteIncrement = (glute_mrv_3x - glute_mev) / (duration - 1);
    }
    if (gluteFreq == 4) {
      gluteIncrement = (glute_mrv_4x - glute_mev) / (duration - 1);
    }
  }

  Mesocycle({
    required this.duration,
    required this.freq,
    required this.mode,
  });
}

void main() {
  runApp(MaterialApp(home: MesocycleView()));
}
// #endregion

class MesocycleView extends StatefulWidget {
  const MesocycleView({Key? key}) : super(key: key);

  @override
  State<MesocycleView> createState() => _MesocycleViewState();
}

class _MesocycleViewState extends State<MesocycleView> {
  late String _gluteMode = 'MAX';
  var _gluteFreq;
  var _fontSize = 20.0;

  
  List<String> frequency = ['1', '2', '3', '4'];


  Mesocycle Meso1 = Mesocycle(
    duration: 8,
    freq: 2,
    mode: 'mass',
  );
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey,
        appBar: AppBar(
          title: Text('Mesocycle View'),
          centerTitle: true,
          backgroundColor: Colors.amber,
        ),
        // #region Grid
        body: Center(
          child: Container(
            //margin: EdgeInsets.fromLTRB(100.0, 10.0, 10.0, 10.0),
            child: GridView(
              children: [
                
                Container(
                    child: DropdownButton(
                  hint: _gluteMode == null
                      ? Center(child: Text('Mode'))
                      : Center(
                          child: Text(
                            _gluteMode,
                            style: TextStyle(
                                color: Colors.black, fontSize: _fontSize),
                          ),
                        ),
                  isExpanded: true,
                  iconSize: 30.0,
                  style: TextStyle(color: Colors.black, fontSize: _fontSize),
                  items: ['Max', 'Min', 'Maintain', 'Off'].map(
                    (val) {
                      return DropdownMenuItem<String>(
                        value: val,
                        child: Text(val),
                      );
                    },
                  ).toList(),
                  onChanged: (val) {
                    setState(
                      () {
                        _gluteMode = val.toString();
                        Meso1.set_Mode('glutes', _gluteMode);
                        Meso1.generateGluteVolume();
                      },
                    );
                  },
                )),
                Container(
                    child: DropdownButton(
                  hint: _gluteFreq == null
                      ? Center(child: Text('Freq.'))
                      : Center(
                          child: Text(
                            _gluteFreq,
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: _fontSize,
                            ),
                          ),
                        ),
                  isExpanded: true,
                  iconSize: 30.0,
                  style: TextStyle(color: Colors.black, fontSize: _fontSize),
                  items: ['2', '3', '4'].map(
                    (val) {
                      return DropdownMenuItem<String>(
                        value: val,
                        child: Text(val),
                      );
                    },
                  ).toList(),
                  onChanged: (val) {
                    setState(
                      () {
                        _gluteFreq = val;
                        Meso1.set_Freq('glutes', _gluteFreq);
                        Meso1.generateGluteVolume();
                      },
                    );
                  },
                )),
... etc

E/flutter (11519): [错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:类型 'double' 不是 'int' 类型的子类型 'value'

E/flutter (11519): #0 List.add (飞镖:core-patch/growable_array.飞镖)

E/flutter (11519): #1 Mesocycle.generateGluteVolume

E/flutter (11519): #2 _MesocycleViewState.build..

E/flutter (11519): #3 State.setState

E/flutter (11519): #4 _MesocycleViewState.build.

E/flutter (11519): #5 _DropdownButtonState._handleTap.

E/flutter (11519): #6 _rootRunUnary (飞镖:async/zone.dart:1434:47)

E/flutter (11519): #7 _CustomZone.runUnary (飞镖:async/zone.飞镖:1335:19)

E/flutter (11519): 异步挂起

Mesocycle class 中,您将 gluteVolume 列表初始化为 <int>[]。但是,稍后您尝试执行此操作:

gluteVolume.add(glute_mev + (gluteIncrement * 3) / 3);

基于Dart documentation/运算符(除法)的结果类型为double。因此,您试图将 double 类型的值添加到 int 列表中 - 您会收到错误消息。

解决问题的最简单方法是将 gluteVolume 列表启动为 <double>[]:

final gluteVolume = <double>[];

如果您想将 gluteVolume 列表保留为 <int>[],您可以使用截断除法 (~/),其中通过四舍五入将小数结果转换为整数零:

gluteVolume.add(glute_mev + (gluteIncrement * 3) ~/ 3);

或者您可以使用 round()/floor() 函数(基于您期望的结果):

gluteVolume.add((glute_mev + (gluteIncrement * 3) / 3).round());
// OR
gluteVolume.add((glute_mev + (gluteIncrement * 3) / 3).round());