lesnitsky / flutter_localstorage

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

Support Isolates #46

Closed uddyami closed 3 years ago

uddyami commented 4 years ago

I'm trying to integrate LocalStorage with an Isolate and keep getting

I/flutter (25457): [Instance of 'PlatformNotSupportedError', #0      DirUtils._getDocumentDir 
package:localstorage/…/directory/localstorage_io.dart:129
I/flutter (25457): <asynchronous suspension>
I/flutter (25457): #1      DirUtils._getFile 
package:localstorage/…/directory/localstorage_io.dart:115
I/flutter (25457): #2      DirUtils.flush 
package:localstorage/…/directory/localstorage_io.dart:46
I/flutter (25457): #3      LocalStorage._flush 
package:localstorage/localstorage.dart:129
I/flutter (25457): #4      LocalStorage._attemptFlush 
package:localstorage/localstorage.dart:122
I/flutter (25457): #5      LocalStorage.setItem 
package:localstorage/localstorage.dart:98
I/flutter (25457): <asynchronous suspension>
I/flutter (25457): #6      Storage.setData 
lesnitsky commented 4 years ago

@uddyami there was a possibility to instantiate a local storage in isolate, but apparently this functionality was broken when web support was added. I'll work to bring isolate support back

uddyami commented 3 years ago

any update for this ?? 🤞

lesnitsky commented 3 years ago

I just checked the code and was able to create an instance of localstorage inside isolate: you need to pass a path to app documents folder (2nd argument of LocalStorage constructor). getApplicationDocumentsDirectory from path_provider doesn't work in isolates, since isolates don't have access to the binary messenger, thus to the underlying platform

Isolate code

// subscribe to messages from main thread

LocalStorage storage;

if (message is Directory) {
  storage = LocalStorage('localstorage', message.path);
  //-------------------------------------^^^^^^^^^^^^^---
  // this prevents localstorage from calling getApplicationDocumentsDirectory
  // which is not available from isolates
}

// now you can call localstorage methods