0

I have static values of RangeSlider now I have to update the values and set them from static to dynamic. but I don't know how to do this please help to show and update the RangeSlider values from the database. I have two vlaues from the database for RangeSlider to start and end in getData() data but I don't know how to initialize the values outside the build method.

values:- start = data[0]['age1'], end = data[0]['age2']
values which comes from databse:- 20 60

Here is my code:

class Age extends StatefulWidget {

Age({Key? key}) : super(key: key);

@override
_Age createState() => _Age();
}


class _Age extends State<Age >{

 var UsrID = Auth.prefs?.getString('usrid');

 var data;

@override
 void initState() {
 super.initState();
 getData();
}

getData() async{
 var res = await http.get(Uri.https('www.*******.com', 
 '/index.php',{'act':'profile','UsrID': '${UsrID}'}));
 data = jsonDecode(res.body);
 print(data);
 setState(() {});
 print(res.body);
}

//var start = data[0]['age1'];
//var end= data[0]['age2'];

//RangeValues _currentRangeValues = RangeValues(start,end);

RangeValues _currentRangeValues = RangeValues(30, 70);

@override
 Widget build(BuildContext context){


return Scaffold(
 Container(
         child: Column(
         crossAxisAlignment: CrossAxisAlignment.start,
         children: [
            Text(
              'Age',
            style: TextStyle(
            color: Color(0xff2c3531),
            ),
          ),
          addVerticalSpace(10),
          RangeSlider(
          activeColor: Color(0xff8f9df2),
          inactiveColor: Color(0xff9a9a9a),
          values: _currentRangeValues,
          max: 100,
          divisions: 5,
          labels: RangeLabels(
          _currentRangeValues.start.round().toString(),
          _currentRangeValues.end.round().toString(),
          ),
          onChanged: (RangeValues values) {
             setState(() {
                _currentRangeValues = values;
             });
          },
          ),
          ],
          ),
          )
      }

Anyone please help how i add dynamic data in RangeValues _currentRangeValues = RangeValues(20, 70);

New error:- Null check operator used on a null value

RAF Algowid
  • 87
  • 2
  • 6

1 Answers1

0

Define _currentRangeValues in the class level

 var data;
 RangeValues? _currentRangeValues;

And initialize the range with getData call

getData() async{
   var res = await http.get(Uri.https('www.*******.com', 
   '/index.php',{'act':'profile','UsrID': '${UsrID}'}));
   data = jsonDecode(res.body);
   _currentRangeValues = RangeValues(data[0][age1], data[0]['age2']);
}

And in order to make an async call in initstate use

@override
void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
     //makes the call when the UI is done.
    getData();
  });
}
Alperen Ekin
  • 258
  • 2
  • 10
  • thanks I added im my code inside getData. but when `_currentRangeValues` defines in `RangeSlider(values : _currentRangeValues)` it give error `The argument type 'RangeValues?' can't be assigned to the parameter type 'RangeValues'.` – RAF Algowid Jun 30 '22 at 11:46
  • For now you can make it RangeSlider(values : _currentRangeValues!) – Alperen Ekin Jun 30 '22 at 11:48
  • it five me Null check operator used on a null value error please see my question i update it. – RAF Algowid Jun 30 '22 at 12:01
  • If you use hot reload after error, does it work correct? To me it seems like the issue is about your response from backend comes while sketching the UI to the screen therefore when the UI is done, you do not have the data you need yet. – Alperen Ekin Jun 30 '22 at 12:12
  • sorry, I can't understand. please give me some solution – RAF Algowid Jul 01 '22 at 04:28