lesnitsky / flutter_localstorage

📦 LocalStorage for Flutter
MIT License
298 stars 65 forks source link

Persistent between runs? #10

Closed mk0y closed 5 years ago

mk0y commented 5 years ago

Nice plugin! Is there a way to persist data between runs in development phase (debug mode)?

Right now each time I run the app local storage is empty.

lesnitsky commented 5 years ago

@markzero do you have empty storage after app livereload? Or do you mean complete reinstall?

mk0y commented 5 years ago

When I re-run the app. Using flutter run. I guess storage is persisted while the app runs?

lesnitsky commented 5 years ago

This is weird, because I don't see storage being erased between flutter runs... What emulator are you using?

On Tue, Mar 12, 2019, 3:52 PM Marko Jakic notifications@github.com wrote:

When I re-run the app. Using flutter run. I guess storage is persisted while the app runs?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/lesnitsky/flutter_localstorage/issues/10#issuecomment-471987566, or mute the thread https://github.com/notifications/unsubscribe-auth/AF-KNnpncydz45k4L5AqitGYIujLUf_bks5vV6MngaJpZM4blgbo .

mk0y commented 5 years ago

I'm using both Simulator for iOS and Android simulator.

I tried with simple code:

@override
  void initState() {
    super.initState();
    Map<String, dynamic> data = _localStorage.getItem('items');
    debugger();
    _localStorage.ready.then((isReady) {
      if (isReady) {
        _localStorage.setItem('items', [1,2,3]);
      }
    });
  }

But every time data is empty (null).

mk0y commented 5 years ago

Sorry it should be List<int> but still, it works while app is running, for example between switching tabs etc, but when I re-run the app it starts with null, ie first call to getItem returns null.

lesnitsky commented 5 years ago

@markzero you should wait for ready in order to get data I recommend using FutureBuilder

class SomeWidget extends StatelessWidget {
    final LocalStorage storage = new LocalStorage('my-name');

    @override
    Widget build(BuildContext context) {
        return FutureBuilder(
            future: storage.ready,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.hasData) {
                      // localstorage is ready
                      return MyPage(storage: strorage);
                  } else {
                      return CircularLoadingIndicator();
                  }
            }
        );
    }
}
lesnitsky commented 5 years ago

@markzero let me know if this example works for you

mk0y commented 5 years ago

@lesnitsky I don't have hasData but just data and it is just AsyncSnapshot<bool>(ConnectionState.done, true, null). There is no data that I set earlier (in initState for example).

I'm using localstorage: ^1.2.0.

You have data even when you re-run the app?

mk0y commented 5 years ago

Upgraded Flutter, seems it works now. When re-run the app there is state persistent now, not sure what was the case...