Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LateInitializationError occurs when used in isolate #8

Closed
tcdspring opened this issue Feb 18, 2022 · 1 comment
Closed

LateInitializationError occurs when used in isolate #8

tcdspring opened this issue Feb 18, 2022 · 1 comment

Comments

@tcdspring
Copy link

E/flutter (24445): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: LateInitializationError: Field '_instance@2084288322' has not been initialized.
E/flutter (24445): #0 SodiumPlatform._instance (package:sodium_libs/src/sodium_platform.dart)
E/flutter (24445): #1 SodiumPlatform.instance (package:sodium_libs/src/sodium_platform.dart:21:41)

sodium_libs version: 1.2.0
flutter version: 2.8

used like this:


await Executor().execute(
    arg1: str1,
    fun1: (String demo) async {
      // this is an isolate
      final sodium = await CustomSodiumInit.init();   // this line occurs LateInitializationError 
      final encData = sodium.crypto.secretBox.easy(...);
      // ...
    },
  );

CustomSodiumInit code:


// ......
static Future<sodium.Sodium> init({
    @Deprecated('initNative is no longer required and will be ignored.')
        bool initNative = true,
  }) =>
      _instanceLock.synchronized(() async {
        if (_instance != null) {
          return _instance!;
        }
        
        // Comment out this line or an exception(Unhandled Exception: UI actions are only available on root isolate) will occur
        // WidgetsFlutterBinding.ensureInitialized();
        _instance = await SodiumPlatform.instance.loadSodium();
        if (!kReleaseMode) {
          if (_instance!.version < _expectedVersion) {
            // ignore: avoid_print
            print(
              'WARNING: The embedded libsodium is outdated! '
              'Expected $_expectedVersion, but was ${_instance!.version}}. '
              '${SodiumPlatform.instance.updateHint}',
            );
          }
        }
        return _instance!;
      });

sodium_libs version 1.1.1 will work, but 1.2.0 not

@Skycoder42
Copy link
Owner

SodiumPlatform.instance is initialized automatically by flutter when the plugin is loaded. This happens as part of the WidgetsBinding.ensureInitialized() in normal code. This is not automatically executed when launching a new isolate, thus it does not get initialized. It did work with older versions, as back then flutter did not support loading dart plugins automatically.

This is not a bug, but expected behaviour. You have to do one of the following things to use the plugin in a different isolate:

  1. Simply call WidgetsBinding.ensureInitialized() in your Isolate
  2. Use a plugin like flutter_isolate that takes care of that for you.
  3. Manually initialize the instance. This requires you to now which platform you are on and select the correct implementation. See https://github.com/Skycoder42/libsodium_dart_bindings/tree/main/packages/sodium_libs/lib/src/platforms where you can find the concrete implementations. For example, to initialize it for Android, you could call SodiumAndroid.registerWith().

@Skycoder42 Skycoder42 added help wanted Extra attention is needed and removed help wanted Extra attention is needed labels Feb 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants