Home > Enterprise >  how to make slider in flutter with API data?
how to make slider in flutter with API data?

Time:08-26

I am trying to implement such logic in slider: i want to pull data from a map which looks like this: {min: 1, max: 16, value: 1.0, step: 1.0}. This data comes from API and I want to manipulate the slider with this data. I did it like in code below, but nothing has happend:

  Slider(
          value: snapshot.data?['value'] ?? 0.toDouble(),
          activeColor: Colors.green,
          max: snapshot.data?['max'] ?? 0.toDouble(),
          min: snapshot.data?['min'] ?? 0.toDouble(),
          inactiveColor: Colors.green,
          label: values.toString(),
          onChanged: (double value) {
            var v = snapshot.data?['value'] ?? 0.toDouble();
            setState(() {
              v = value;
            });
          },
        ),

how can I achive this? Also it gives me an error:

type 'int' is not a subtype of type 'double'

CodePudding user response:

Maybe the error come from api result so try do this:

Slider(
                        value: double.parse(snapshot.data?['value'].toString()?? '0'),
                        activeColor: Colors.green,
                        max: double.parse(snapshot.data?['max'].toString()?? '0')  ,
                        min: double.parse(snapshot.data?['min'].toString()?? '0'),
                        inactiveColor: Colors.green,
                        label: values.toString(),
                        onChanged: (double value) {
                          var v = double.parse(snapshot.data?['value'].toString()?? '0');
                          setState(() {
                            v = value;
                          });
                        },
                      )

CodePudding user response:

I think the api return value int causing the issue. You can do

Slider(
  value: double.tryParse("${snapshot.data?['value']}") ?? 0,
  activeColor: Colors.green,
  max: double.tryParse("${snapshot.data?['max']}") ?? 0,
  min: double.tryParse("${snapshot.data?['min']}") ?? 0,
  inactiveColor: Colors.green,
  • Related