lesnitsky / flutter_localstorage

📦 LocalStorage for Flutter
MIT License
301 stars 60 forks source link

Localstorage doesn't persist #60

Closed tree1891 closed 2 years ago

tree1891 commented 3 years ago

In fcm background handler, set some data to localstorage, but can't read it after app resume. It needs reload function.

tylersavery commented 3 years ago

having this same issue on Android only.

dcbacarro commented 3 years ago

Any update on this?

jmls commented 3 years ago

I'm having this issue as well

(update) https://github.com/lesnitsky/flutter_localstorage/issues/49#issuecomment-676146168 fixes it for me

jabirmayar commented 2 years ago

any update on this?

elkattan commented 2 years ago

I also faced this issue, mainly because i was trying to load the JSON files before initialization compelete, so the solution in my case was initializing the app before loading data.

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // <-- Adding this line to initialize my app first
  await initServices(); // Then loading the data will work properly
  runApp(const GetMaterialApp(home: Home()));
}
celinaT commented 2 years ago

I did what you said in comment #49 but it does not work. As you can see in the log. Any plans to fix this?

// app opened D/FLTFireMsgReceiver(10953): broadcast received for message I/flutter (10953): Notifications exist in local storage I/flutter (10953): 11 I/flutter (10953): Notifications exist in local storage I/flutter (10953): 12

// app in background -> 12! I/flutter (10953): Handling a background message 0:1638459902632656%a242c321a242c321 W/FirebaseMessaging(10953): Unable to log event: analytics library is missing I/flutter (10953): Notifications exist in local storage I/flutter (10953): 12 I/flutter (10953): Handling a background message 0:1638459923179893%a242c321a242c321 I/flutter (10953): Notifications exist in local storage W/FirebaseMessaging(10953): Unable to log event: analytics library is missing I/flutter (10953): 13

// opening app again 13 again, even though 13 was saved above I/flutter (10953): Notifications exist in local storage I/racker.bittrac(10953): WaitForGcToComplete blocked Background on ProfileSaver for 8.033ms I/flutter (10953): 13 I/flutter (10953): Notifications exist in local storage I/flutter (10953): 14

celinaT commented 2 years ago

Here you go, an even better example with a counter and with the increment counter function.

The function:

` static Future\<int> incrementCounter() async{ await storage.ready;

int counter = await getCounter();
await storage.ready;

counter += 1;

await storage.setItem(COUNTER, counter);
await storage.ready;

return counter;

} `

And the output:

I/flutter (10953): Counter from local storage 1, mode: app opened I/flutter (10953): Counter from local storage 2, mode: app opened I/flutter (10953): Counter from local storage 3, mode: app opened I/flutter (10953): Counter from local storage 4, mode: app opened I/flutter (10953): Counter from local storage 5, mode: app opened I/flutter (10953): Counter from local storage 7, mode: background mode I/flutter (10953): Counter from local storage 6, mode: app opened

lesnitsky commented 2 years ago
void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // <-- make sure this is executed before any calls to the `LocalStorage`
  // ...
}
await storage.ready; // <-- make sure this is called somewhere in your code before any other calls to the `LocalStorage`
celinaT commented 2 years ago

This does not solve the issue. There is an error with a cache and the map which you are using.