samples icon indicating copy to clipboard operation
samples copied to clipboard

[Veggie Season] The veggies saved to Garden are deleted when app is closed

Open kjain333 opened this issue 3 years ago • 6 comments

A possible bug in the Veggie Season application is when we close our application the data we save to the garden gets deleted. A possible fix for this may be to use shared preferences to store favorite veggies instead of the app state.

kjain333 avatar Oct 22 '20 09:10 kjain333

That's definitely not the desired behavior. VeggieSeason should be using shared_preferences to store the data on device between executions.

Can you provide a little more information? What device and iOS version were you using when you saw this happen, for instance?

RedBrogdon avatar Oct 22 '20 20:10 RedBrogdon

Respected sir, I am really sorry for not providing this information before but due to system constraints I was presently just able to run this code on my android with android version 10 but looking at the code itself since we are using shared preferences to store the preferred calorie count and category of veggie why not we use the same for garden as well?

kjain333 avatar Oct 23 '20 03:10 kjain333

Unlike the persistent store of data in the shared_preference for [_caloriesKey] and [_preferredCategoriesKey] the data for My Garden is not stored or saved anywhere! (Tested on both iOS and android) Below code reference --> lib/data/preferences.dart/[line 57]

  Future<void> _saveToSharedPrefs() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_caloriesKey, _desiredCalories);

    // Store preferred categories as a comma-separated string containing their
    // indices.
    await prefs.setString(_preferredCategoriesKey,
        _preferredCategories.map((c) => c.index.toString()).join(','));
  }

Here only the values of calories target and preferred categories are stored and saved after state of the app is closed. The data of [My Garden] is not stored anywhere that's why the favourite data for my garden is not saved and gets renewed to null every time the app restarts. This is probably not what a user would want!

 1-User marks veggies as      ->      2-Data gets saved and shows in       ->      3-User restarts the app and the data 
       [Save to Garden]                                 [My Garden]                for [My Garden] gets renewed to null                        

--------------->--------------->

@RedBrogdon I would like to fix this issue either by using shared preferences for storing the [My Garden] data or storing data directly in the device whichever way suits the best for this purpose.

rj-since-2000 avatar Oct 30 '20 21:10 rj-since-2000

Hi, @rj-since-2000. The #433 is working to implement state restoration & taking the veggie seasons app to the experimental directory. I think this issue will also be covered. But surely, @RedBrogdon will give the final call on it. After that, you work on resolving the issue.

tusharojha avatar Oct 31 '20 04:10 tusharojha

@rj-since-2000 while that PR will restore the garden when the OS kills the app while running in the background, it will not persist your favorite veggies when the user actively quits the app. For that, the garden veggies will have to be stored in shared_preferences, as suggested earlier.

goderbauer avatar Oct 31 '20 07:10 goderbauer

Why was the distinction made between user killed and system killed for the RestorationMixin? Why would Flutter not save/restore the state whenever setState() is called on a Restorable property, period?

jeffreydwalter avatar Nov 10 '22 16:11 jeffreydwalter