flutterdartdio

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: dio.interceptors.add(CookieManager(_cookieJar));

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 {
  initLocator();

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

  // ...
}

void main() async {
  await initStuff();

  runApp(MyApp());
}

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
      ..interceptors.add(CookieManager(_cookieJar));
  }

The error I'm getting:

E/flutter (24953): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (24953): #0      MethodChannel.binaryMessenger
package:flutter/…/services/platform_channel.dart:142
E/flutter (24953): #1      MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:148
E/flutter (24953): #2      MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:331
E/flutter (24953): #3      MethodChannelPathProvider.getApplicationDocumentsPath
package:path_provider_platform_interface/src/method_channel_path_provider.dart:50
E/flutter (24953): #4      getApplicationDocumentsDirectory
package:path_provider/path_provider.dart:138
E/flutter (24953): #5      initStuff
package:proj/main.dart:20
E/flutter (24953): #6      main
package:proj/main.dart:63
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      _CustomZone.run (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)


Solution

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

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

    Suggestions:

    1. Using a StatefulWidget is a bit more work - but it makes sense here. I think it also happens to resolve the problem for you.

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

      https://pub.dev/documentation/path_provider/latest/path_provider/getApplicationDocumentsDirectory.html

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