0

what im trying to do is passing a value hour into another page, into the dropdownbar for user to update the hour, but when i tried to use sharedpreference to store the data and get the data out. I am able to print out the data during the print(get) but the dropdownvalue which assigned the get value, it is empty and get assigned the initial value which is 1.

class UpdateService extends StatefulWidget {
  final String serviceName;
  final String serviceDetails;
  final String serviceImage;

  UpdateService({required this.serviceName, required this.serviceDetails,
  required this.serviceImage});

  @override
  State<UpdateService> createState() => UpdateServiceState();
}
String dropdownvalue = '1';
class UpdateServiceState extends State<UpdateService> {
  TextEditingController serviceDetailsController = TextEditingController();
  TextEditingController hoursNeedController = TextEditingController();

  var items = ['1', '2', '3', '4',
    '5', '6', '7', '8', '9'];

  @override
  void initState(){
    serviceDetailsController = TextEditingController(text: widget.serviceDetails.toString());
    loadPref();
    super.initState();
  }

  loadPref() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    //Return String
    String get = prefs.getString('hour').toString() ?? "none";
    dropdownvalue = get;
    print(get);
  }

  Widget build(BuildContext context) {

DropdownButton(
                  value: dropdownvalue,
                  items: items.map((String items){
                    return DropdownMenuItem(
                        value: items,
                        child: Text(items));
                  }).toList(),
                  onChanged: (String? newValue){
                    setState(() {
                      dropdownvalue = newValue!;
                    });
                  }),

well i run out of ideas on how to pass it, i hope someone can help me

Lim
  • 31
  • 6

1 Answers1

0

String get = prefs.getString('hour').toString();

will try to get it if it's stored, but you don't have it saved yet so it throw null and you can't assign null to a non nullable Object which is String in your case, so you need to make it nullable and give it a default value when it's null, so:

replace:

    String get = prefs.getString('hour').toString();

with this:

    String? get = prefs.getString('hour').toString() ?? "default value here";
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • oh sorry, i think i ask it wrongly, i have edited the question, what i dun understand about the code is that i am able to print out the prefs.getString value which is 3, yet when i assigned it to dropdownvalue, it seems to have not assigned anything yet still have the initial value 1 – Lim Nov 12 '22 at 17:33
  • oh, sorry for being late, it seems that you're storing that value is assigned but the state is not updated, since the load method is Future, putting it inside initState will make it execute late, so replace this: dropdownvalue = get; with this: SetState((){ dropdownvalue = get;}) – Gwhyyy Nov 12 '22 at 20:00
  • lemme know if this fixed it or no – Gwhyyy Nov 12 '22 at 20:00
  • oh im sorry, i miss out your comments, it works but i don't understand it, isn't initState suppose to run first so the value will get assigned ? btw thanks a lot man, like really thanks – Lim Nov 13 '22 at 08:40
  • Hi, the initState is not asynchronous, the code init will execute and directly will call the build, the loadPref is asynchronous, so it takes some time, even if it's not really visible to you, but it needs time before it get the sharedPreference instance, until that the get will be null so on that moment get will be the default value we set, then after it gets the instance it will update the satte with the actual stored value – Gwhyyy Nov 13 '22 at 10:18
  • oh thats why, thanks a lot – Lim Nov 13 '22 at 16:05