Iago Gonalves
Iago Gonalves

Reputation: 29

How to make a map flutter?

In a part of my flutter code I have 31 cylinders in which each has its weight and volume. I would like to make a kind of map to optimize the code. Here is the code:

  List<int> cilindro1 = [4145, 2100];
  List<int> cilindro2 = [4405, 2085];
  List<int> cilindro3 = [4140, 2095];
  List<int> cilindro4 = [4090, 2086];
  List<int> cilindro5 = [4195, 2089];
  List<int> cilindro6 = [4120, 2088];
  List<int> cilindro7 = [4170, 2075];
  List<int> cilindro8 = [4160, 2076];
  List<int> cilindro9 = [4100, 2092];
  List<int> cilindro10 = [4220, 2079];
  List<int> cilindro11 = [4100, 2094];
  List<int> cilindro12 = [4225, 2090];
  List<int> cilindro13 = [4150, 2100];
  List<int> cilindro14 = [4215, 2088];
  List<int> cilindro15 = [4100, 2090];
  List<int> cilindro16 = [4160, 2089];
  List<int> cilindro17 = [4105, 2089];
  List<int> cilindro18 = [4265, 2079];
  List<int> cilindro19 = [4365, 2076];
  List<int> cilindro20 = [4235, 2089];
  List<int> cilindro21 = [4155, 2096];
  List<int> cilindro22 = [4150, 2100];
  List<int> cilindro23 = [4115, 2098];
  List<int> cilindro24 = [4160, 2099];
  List<int> cilindro25 = [4285, 2109];
  List<int> cilindro26 = [4185, 2087];
  List<int> cilindro27 = [4490, 2075];
  List<int> cilindro28 = [4220, 2087];
  List<int> cilindro29 = [4120, 2114];
  List<int> cilindro30 = [4255, 2087];
  List<int> cilindro31 = [2255, 801]; 

This code is where I use the cylinders:

dropdown() {
    return Center(
      child: DropdownButton<String>(
        items: [
          DropdownMenuItem<String>(
            child: Text('Cilindro 1'),
            value: '$cilindro1',
          ),
          DropdownMenuItem<String>(
            child: Text('Cilindro 2'),
            value: '$cilindro2',
          ),
          DropdownMenuItem<String>(
            child: Text('Cilindro 3'),
            value: '$cilindro3',
          ),
          DropdownMenuItem<String>(
            child: Text('Cilindro 4'),
            value: '$cilindro4',
          ),
          DropdownMenuItem<String>(
            child: Text('Cilindro 5'),
            value: '$cilindro5',
          ),
          ...
          DropdownMenuItem<String>(
            child: Text('Cilindro 31'),
            value: '$cilindro31',
          ),
        ],
        onChanged: (String value) {
          setState(() {
            if (value == '$cilindro31') {
              var peso = value.substring(1, 5);
              var volume = value.substring(7, 10);
              _peso = int.parse(peso);
              _volume = int.parse(volume);
            } else {
              var peso = value.substring(1, 5);
              var volume = value.substring(7, 11);
              _peso = int.parse(peso);
              _volume = int.parse(volume);
            }
            _index = value;
          });
        },
        hint: Text('Selecione o Cilindro'),
        isExpanded: true,
        value: _index,
      ),
    );
  } 

I would like to use a way in which they optimize the list and dropdown code however, I can't do it. Would you help me?

Upvotes: 0

Views: 48

Answers (1)

Pegasis
Pegasis

Reputation: 1424

You can create a map which maps a cylinder's name to it's specs (weight and volume).

The type of the name is String and the type of the specs is List<Int> (actually you should create a class for it but it's not relevent to what you are asking for.)

So the type of the map will be Map<String,List<Int>>

You can write it like this:

Map<String,List<Int>> cylinders = {
    "cilindro1": [4145, 2100],
    "cilindro2": [4405, 2085],
    .....
};

When using cylinders, you can map it to items which DropdownButton<String> takes by:

cylinders.entries.map((entry) => DropdownMenuItem<String>(
    child: Text(entry.key),
    value: '${entry.value}',
))

Upvotes: 1

Related Questions