i need to use a future builder to get the value of a radio button form shared preferences. but the error occurs when i open the page of the radio button is:
Another exception was thrown: Null check operator used on a null value
the radio button widget is:
FutureBuilder<bool>(
future: getDayNotificationIsOn(),
builder: (context, snapshot) {
return NeumorphicSwitch(
value: snapshot.data!,
style: NeumorphicSwitchStyle(
thumbShape: NeumorphicShape.flat,
activeTrackColor: const Color(0xff257864),
),
onChanged: (value) async {
setState(() {});
//sharedPreferences
await setDayNotificationIsOn(value);
},
);
}),
the function used in future builder to get the value of the radio button:
Future<bool> getDayNotificationIsOn() async {
SharedPreferences _pref = await SharedPreferences.getInstance();
return _pref.getBool('dayNotificationIsOn') ?? false;
}
You should add a check before NeumorphicSwitch
to make sure the snapshot has data by adding if (snapshot.hasData)
.