Initialize Dio with persistent cookie at the start of the program


I’m using Dio in my app and I want it to save cookies. It turns out I have to download three additional packages to do that: cookie_jar, dio_cookie_manager and path_provider.

I need to:

  1. get application document directory:
    Directory docDir = await getApplicationDocumentsDirectory();

  2. pass it to PersistCookieJar
    PersistCookieJar(storage: FileStorage(appDocDir.path + '/.cookies/'));

  3. and add it to interceptors:

My problem is with the first part. For some reason, getApplicationDocumentsDirectory(); throws an error when not used inside a StatefulWidget widget.

I have two questions:

  1. Why on earth would getting a path require a StatefulWidget?
  2. How can I do what I want? I need to initialize Dio as early as possible so that by the time the UI loads, the user is already (or almost) logged in. Plus, I don’t want to put the logic of initiating the client object inside the UI code, that might for example cause it to be initiated multiple times whenever the widget is created.

What my code looks like right now:

Future<void> initStuff() async {

  Directory docDir = await getApplicationDocumentsDirectory(); // throws
  print('docDir = ${docDir.path}');
  Client cli = Client(docDir);

  // ...

void main() async {
  await initStuff();


Client constructor:

  Client(Directory appDocDir) {
    final _cookieJar = PersistCookieJar(storage: FileStorage(appDocDir.path + '/.cookies/'));

    dio = Dio()
      ..options.baseUrl = Utils.SERVER_ADDRESS
      ..options.sendTimeout = 5000
      ..options.receiveTimeout = 5000
      ..options.connectTimeout = 5000

The error I’m getting:

E/flutter (24953): [ERROR:flutter/lib/ui/] Unhandled Exception: Null check operator used on a null value
E/flutter (24953): #0      MethodChannel.binaryMessenger
E/flutter (24953): #1      MethodChannel._invokeMethod
E/flutter (24953): #2      MethodChannel.invokeMethod
E/flutter (24953): #3      MethodChannelPathProvider.getApplicationDocumentsPath
E/flutter (24953): #4      getApplicationDocumentsDirectory
E/flutter (24953): #5      initStuff
E/flutter (24953): #6      main
E/flutter (24953): #7      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter (24953): #8      _rootRun (dart:async/zone.dart:1354:13)
E/flutter (24953): #9 (dart:async/zone.dart:1258:19)
E/flutter (24953): #10     _runZoned (dart:async/zone.dart:1789:10)
E/flutter (24953): #11     runZonedGuarded (dart:async/zone.dart:1777:12)
E/flutter (24953): #12     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:138:5)
E/flutter (24953): #13     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (24953): #14     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)


OK: Thank you for updating your post.

The error is: Unhandled Exception: Null check operator used on a null value

It’s coming from the 3rd party library call getApplicationDocumentsDirectory(), and it’s occurring because you’re calling a (currently uninitialized) value with null-safety enabled (a Good Thing!).


  1. Using a StatefulWidget is a bit more work – but it makes sense here. It also happens to resolve the problem for you, correct?

  2. You might try making docDir nullable (e.g. String? path). This is exactly what the getApplicationDocument page shows:

Future<Directory> getApplicationDocumentsDirectory() async {
  final String? path = await _platform.getApplicationDocumentsPath();
  if (path == null) {
    throw MissingPlatformDirectoryException(
        'Unable to get application documents directory');
  return Directory(path);
  1. Finally, check out these links:

Answered By – paulsm4

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published