Search code examples
flutterdartdirectorybloc

HydratedBloc expects a type HydratedStorageDirectory but getApplicationDocumentsDirectory returns a type of Directory


I am new to Flutter and currently trying to learn Bloc and HydratedBloc. I am trying to initialize the storage using getApplicationDocumentDirectory() and use it for the storageDirectory parameter in HydratedStorage.build but I keep getting a type mismatch error. Any help or clarification would be greatly appreciated!

Here is what I have so far: HydratedBloc.storage = await HydratedStorage.build( storageDirectory: await getApplicationDocumentsDirectory(), );

I have tried going throught the documentation on hydrated_bloc on pub.dev, and it seems like this error should not be occuring. Here is a relevant snippet from those docs: final storage = await HydratedStorage.build( storageDirectory: await getApplicationDocumentsDirectory(), );


Solution

  • That is probably some outdated documentation. The current way seems to be to surround the getApplicationDocumentsDirectory with a HydratedStorageDirectory constructor.

    HydratedBloc.storage = await HydratedStorage.build(
      storageDirectory: HydratedStorageDirectory(
        (await getApplicationDocumentsDirectory()).path,
      ),
    );