firstfloorsoftware / flutter_sodium

Flutter bindings for libsodium
BSD 3-Clause "New" or "Revised" License
102 stars 46 forks source link

MissingPluginException, Flutter 0.8.2 and up #10

Closed trillom closed 5 years ago

trillom commented 5 years ago

I'm receiving the following error: MissingPluginException(No implementation found for method crypto_box_keypair on channel flutter_sodium)

I've tried reloads, restarts, starting a new project with JUST the offending code, deleting generated files and rebuilding, nothing works. Code below.

Example class:

import 'package:flutter_sodium/flutter_sodium.dart';

class KeygenTest {
  KeygenTest();

  Future<String> generateKeys () async {
    KeyPair keys = await CryptoBox.generateKeyPair();
    print (keys.toString());
    return "generated";
  }
}

Unit test:

import 'package:flutter_test/flutter_test.dart';
import 'keygen.dart';

void main() {
  group("keygen", () {
    test("generate key", () async {
      KeygenTest test = new KeygenTest();
      await test.generateKeys();
    });
  });
}

Full output:

$ flutter test lib/keygen.test.dart
00:12 +0 -1: keygen generate key [E]
  MissingPluginException(No implementation found for method crypto_box_keypair on channel flutter_sodium)
  package:flutter/src/services/platform_channel.dart 278:7  MethodChannel.invokeMethod
  ===== asynchronous gap ===========================
  dart:async                                                _AsyncAwaitCompleter.completeError
  package:flutter/src/services/platform_channel.dart        MethodChannel.invokeMethod
  ===== asynchronous gap ===========================
  dart:async                                                _asyncThenWrapperHelper
  package:flutter/src/services/platform_channel.dart        MethodChannel.invokeMethod
  package:flutter_sodium/flutter_sodium.dart 554:10         Sodium.cryptoBoxKeypair
kozw commented 5 years ago

Flutter plugins (including flutter_sodium) don't work in unit tests. Tests run completely headless, in a dart only environment. There's no Android or iOS runtime available.

You can mock a plugin, see https://docs.flutter.io/flutter/services/MethodChannel/setMockMethodCallHandler.html