skip to Main Content

I am making Notes App using Flutter which has 2 screens one is the view card screen and the other is home screen.

I want the notes to be stored locally on storage and enable read-write to perform operations. Right now I have the option to create note and save it in some variable (which is reset to no notes when the app is reopened after the exit). If anyone knows this answer, please help me out.

Thank you.

I tried to find out ways of solving this problem but couldn’t get exact solution to my problem.

2

Answers


  1. You can user SharedPreference for store data as locally.

    1.First you should add SharedPreference dependency. follow bellow steps for do it.

    You can got it using this [link][1]
    

    Or you can install it by you’r terminal go to you flutter proejct and open terminal and copy flutter pub add shared_preferences and put it you’r terminal. if you’r done correctly you can see it cheking you’r pubspec.ymal file like bellow.

    Look at the line number 51. don’t care other’s pubspec.ymal

    2.Now you can use sharedpreference in you’r proect. look at bellow code segment.

    Code example

    Login or Signup to reply.
  2. It is Simple, just follow the steps

    1. First, import the dependency Shared preferences form
      https://pub.dev/packages/shared_preferences and check the details about it

    2. Now, you can use the shared_preferences package to store and retrieve data. Here’s an example:

    class _HomeScreenState extends State<HomeScreen> {
      TextEditingController _noteController = TextEditingController();
      List<String> notes = [];
    
      @override
      void initState() {
        super.initState();
        _loadNotes();
      }
    
      _loadNotes() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          notes = prefs.getStringList('notes') ?? [];
        });
      }
    
      _saveNotes() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setStringList('notes', notes);
      }
    1. Just put these code to Textfield and start modification
     TextField(
                controller: _noteController,
                decoration: InputDecoration(
                  hintText: 'Enter your note',
                ),
              ),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    notes.add(_noteController.text);
                    _noteController.clear();
                    _saveNotes();
                  });
                },
                child: Text('Add Note'),
              ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search