简体   繁体   中英

How to update flutter RangeSlider values which comes from database

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:- 用于空值的空检查运算符

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();
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM