1

I want to update the RangeSlider values from the database. but I don't know how to do this please help to show and update the RangeSlider values from the database. I have two values from the database for RangeSlider to start and end which i set in getData() data but when I initialize the values in Rnageslider it gives me the error The argument type 'RangeValues?' can't be assigned to the parameter type 'RangeValues'. and also in RangeLabels(_currentRangeValues.start.round().toString(),_currentRangeValues.end.round().toString(),) In RangeLabels it gives me an error:- The property 'start' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!'). and same for end

values:- _currentRangeValues = RangeValues(data[0]['age1'], data[0]['age2']);
values which comes from databse:- 20 60 in getDData() function

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;

 RangeValues? _currentRangeValues;

@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);
 _currentRangeValues = RangeValues(data[0]['age1'], data[0]['age2']);
 setState(() {});
 print(res.body);
}


//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 to initialize dynamic data in `RangeValues

Here is error in RangeSlider() widget :- Eror durind initialize the values

RAF Algowid
  • 87
  • 2
  • 6

1 Answers1

0

You have to use Nullable types to avoid this issue.

change RangeValue to RangeValue?.

and you have to use ! at _currentRangeValue!.start.round().toString()

you can find more info on null safety Here

ravipatel0508
  • 358
  • 2
  • 14
  • thank you for reply when i load the screen it gives me error of `Unhandled Exception: type 'String' is not a subtype of type 'double'` in `_currentRangeValues = RangeValues(data[0]['age1'], data[0]['age2']);` at `getData()` function can you tell me how to fix this and also change the code of `_currentRangeValue` to `_currentRangeValue!` and `RangeValue` to `RangeValue? ` please in my question. I edit it and help me. – RAF Algowid Jul 01 '22 at 07:54
  • And also `_currentRangeValues!` gives me error of `Null check operator used on a null value` – RAF Algowid Jul 01 '22 at 07:56
  • if you are getting a type error then you have to cast your String to Double `double.parse(stringName)`. for `second error null operator used on null` you can give some initial values when you declare your variable. – ravipatel0508 Jul 01 '22 at 08:01
  • ok i got this `double.parse(stringName)` but how to set some initial value at this `RangeValues? _currentRangeValues;` ? – RAF Algowid Jul 01 '22 at 08:21
  • I give some initial values like this outside the build method `RangeValues? _currentRangeValues = RangeValues(20, 50);` it also give me error of `Null check operator used on a null value` – RAF Algowid Jul 01 '22 at 08:25
  • now if you are giving initial values so there is no need to add `?`. change it to `RangeValue`. and then stop the program and run it again. if it still gives an error then provide more detail of the error(add stacktrace in question). – ravipatel0508 Jul 01 '22 at 08:40