Home > other >  How to pass API data value into the sfslider range in Flutter?
How to pass API data value into the sfslider range in Flutter?

Time:07-06

How to pass the API data value into the sfslider if I pass the static value to the slider it can be draggable. But if I give the API data value directly it can drag but not update the new value and return to the API response value position.

I saw some of the solutions they said declare a static value outside of a widget, it works fine. But I need to use API values, How to do it? Somebody can help me!

 double _value = 40.0;
                
                @override
                Widget build(BuildContext context) {
                  return MaterialApp(
                      home: Scaffold(
                          body: Center(
                           
               FutureBuilder(
               future: propertycall(),
               // ignore: missing_return
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                 if (snapshot.hasData) {
                    return SfSlider(
                   shouldAlwaysShowTooltip: true,
                   activeColor: secondarycolor,
                   min: snapshot.data["set-parameters"]["mortgage_interest_rate"]["min_value"],
                   max: snapshot.data["set-parameters"]["mortgage_interest_rate"]["max_value"],
                    value: _value,       //issue occur here 
    // value:snapshot.data["set-parameters"]["mortgage_interest_rate"]
    //["default_value"]    
                                                                                                                    
                                                                                                            
             interval: snapshot.data["set-parameters"]["mortgage_interest_rate"]
                          ["steps_value"], 
             showTicks: false,
             showLabels: false,
             enableTooltip: true,
             numberFormat: NumberFormat(),                                             
                                                                    
             onChanged: (new_value) {
                  setState(() {
                   _value = new_value; // issues!!
    
         //API value working but not able to drag slider, 
         //if I give static value from outside of a widget it works                                                  
                                                          
        //  _value=snapshot.data["set-parameters"]["mortgage_interest_rate"]
    //["default_value"]  
                                                          
                                                                                                                                                        },
);
  },

 ),
},
},
);       
   ),
          ),
                     
                  );
                }
            
           

CodePudding user response:

Make _value nullable and use it when not null.

double? _value = null;

...

value: _value ?? snapshot.data["set-parameters"]["mortgage_interest_rate"]
  • Related