Home > Software design >  Dropdown Button Usage in Flutter - Unhandled Exception: type 'double' is not a subtype of
Dropdown Button Usage in Flutter - Unhandled Exception: type 'double' is not a subtype of

Time:03-19

I'm trying to use a dropdown button to change some values and then using those values to change object properties and call object methods but im getting the following errors when i attempt to do so,

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): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'double' is not a subtype of type 'int' of 'value'

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

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 (dart:async/zone.dart:1434:47)

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

E/flutter (11519): asynchronous suspension

CodePudding user response:

Inside the Mesocycle class, you initialise the gluteVolume list as <int>[]. However, later you try to execute this:

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

Based on the Dart documentation, the result type of / operator (division) is double. Thus, you are trying to add a double type value to an int list - you get the error.

The easiest way to resolve an issue is to initiate the gluteVolume list as <double>[]:

final gluteVolume = <double>[];

If you would like to keep your gluteVolume list as <int>[], you can use the truncating division (~/) where a fractional result is converted to an integer by rounding towards zero:

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

Or you could use the round()/floor() functions (based on the result you expect):

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