skip to Main Content

When a value is selected inside the time picker and then exiting the application and returning again, the value is retrieved. The first value. How can I save it inside sharedpreferences? I tried to store it as a string, but to no avail.

2

Answers


  1. Chosen as BEST ANSWER
        //This code
    
    
    
    
    
    
    
    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    import 'package:flutter_localization/flutter_localization.dart';
    TimeOfDay _timeOfDaybgm =  const TimeOfDay(hour: 5, minute: 30);
    void main() {
       runApp(
          
       const  MaterialApp(
                
          debugShowCheckedModeBanner: false,
                
       home: MyApp()
       
       
       ));
    }
    
    class MyApp extends StatefulWidget {
      const MyApp({super.key});
    
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
    
           debugShowCheckedModeBanner: false,
    
          title: 'Flutter Demo',
          theme: ThemeData(
    
            useMaterial3: true,
          ),
          home: const MyHomePage(title: 'لا اله الا الله '),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
    
    
      @override
      void initState() {
    
    
        super.initState();
    
    
    
        getSavedData();
      }
    
      getSavedData() async {
        final pref = await SharedPreferences.getInstance();
    
       // _timeOfDaybgm=TimeOfDay.fromDateTime(DateTime.parse(pref.getString('_timeOfDay')??_timeOfDay.toString()))
    
    
    
    
    
        ;
    
    
        setState(() {});
      }
    
      @override
      void dispose(){
    
    
    
        super.dispose();
    
    
      }
    
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: AppBar(
    
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
    
            title: Text(widget.title),
          ),
          body:  Center(
    
            child: Column(
    
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
    
                TextButton(onPressed:() async {
                  final TimeOfDay?timeOfDay = await showTimePicker(
                    context: context,
                    initialTime:_timeOfDaybgm,
                  );
                  if (timeOfDay!= null) {
                    setState(() {
                      _timeOfDaybgm=timeOfDay;
    
                    });
                    FocusScope.of(context).unfocus();
                    final prefs = await SharedPreferences.getInstance();
                    await prefs.setString('_timeOfDay',_timeOfDaybgm.hour.toString());
                  }
    
                  //notfi();
                }, child:const Text("Added time"),
    
    
                ),
                Center(child: Text(_timeOfDaybgm.format(context).toString(),style: TextStyle(color: const Color.fromARGB(255, 5, 2, 2)),)),
    
              ],
            ),
          ),
    
        );
      }
    }
    

  2. Could you please try the solution provided here? If it doesn’t work, feel free to share the code, and I’ll be happy to assist you.

    stackoverflow

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search