lesnitsky / flutter_localstorage

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

Migrating to latest api #104

Open BrainRayChristensen opened 3 months ago

BrainRayChristensen commented 3 months ago

I used to access local storage like this: LocalStorage(Constants.storage) but with the updated api there's no way to access the data that used to be at that location. Is there a way to migrate the old data so users in my app don't lose their configs?

BrainRayChristensen commented 3 months ago

I found a (not so great) solution. I had to add a dependency on path_provider and then I added the following code to my main method:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  String appDirectory = (await getApplicationDocumentsDirectory()).path;
  File oldLocalStorageFile = File(appDirectory + '/' + Constants.storage);
  File newLocalStorageFile = File(appDirectory + '/' + 'storage-61f76cb0-842b-4318-a644-e245f50a0b5a.json');
  if (oldLocalStorageFile.existsSync()){
    if (newLocalStorageFile.existsSync()){
      newLocalStorageFile.deleteSync();
    }
    oldLocalStorageFile.copySync(newLocalStorageFile.path);
    oldLocalStorageFile.deleteSync();
  };
  await initLocalStorage();

  runApp(MyApp());
}

I'd much prefer to have an option such as : await initLocalStorage(Constants.storage); or await initLocalStorage(oldFileLocation: Constants.storage)

lesnitsky commented 3 months ago

I will add migration functionality to the API

RodideBoer commented 4 weeks ago

What if you used to have multiple different local storages? Would that mean that all those should be migrated into a single instance/file?

ChristianMaidhof commented 3 weeks ago

I will add migration functionality to the API

Are there any updates about the migration solution?

im-web commented 2 weeks ago

Up I use localstorage in that way final storage = LocalStorage(LocalStorageKey.address); or like that final ready = await storage.ready; if (ready) { await storage.setItem(kLocalKey['countries']!, countries); } but there is not workaround. Thanks for help

drvela commented 1 week ago

I found a (not so great) solution. I had to add a dependency on path_provider and then I added the following code to my main method:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  String appDirectory = (await getApplicationDocumentsDirectory()).path;
  File oldLocalStorageFile = File(appDirectory + '/' + Constants.storage);
  File newLocalStorageFile = File(appDirectory + '/' + 'storage-61f76cb0-842b-4318-a644-e245f50a0b5a.json');
  if (oldLocalStorageFile.existsSync()){
    if (newLocalStorageFile.existsSync()){
      newLocalStorageFile.deleteSync();
    }
    oldLocalStorageFile.copySync(newLocalStorageFile.path);
    oldLocalStorageFile.deleteSync();
  };
  await initLocalStorage();

  runApp(MyApp());
}

I'd much prefer to have an option such as : await initLocalStorage(Constants.storage); or await initLocalStorage(oldFileLocation: Constants.storage)

Hi there, how does this work? It seems to me you are deleting the new local storage file but also copying and deleting it as the old local storage file?

drvela commented 1 week ago

I found a (not so great) solution. I had to add a dependency on path_provider and then I added the following code to my main method:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  String appDirectory = (await getApplicationDocumentsDirectory()).path;
  File oldLocalStorageFile = File(appDirectory + '/' + Constants.storage);
  File newLocalStorageFile = File(appDirectory + '/' + 'storage-61f76cb0-842b-4318-a644-e245f50a0b5a.json');
  if (oldLocalStorageFile.existsSync()){
    if (newLocalStorageFile.existsSync()){
      newLocalStorageFile.deleteSync();
    }
    oldLocalStorageFile.copySync(newLocalStorageFile.path);
    oldLocalStorageFile.deleteSync();
  };
  await initLocalStorage();

  runApp(MyApp());
}

I'd much prefer to have an option such as : await initLocalStorage(Constants.storage); or await initLocalStorage(oldFileLocation: Constants.storage)

sorry I read the copy function backwards, this makes sense and helps a lot until migration functionality is added. thank you :)