bindInstance static method

void bindInstance(
  1. SuperuserInterface? suInterface
)

Attach specified suInterface as data source of superuser property.

If suInterface is null, it binds platform specified SuperuserInterface automatically.

When this invoked in neither Windows, macOS or Linux platform, it throws UnsupportedError.

If suInterface is a member of SuperuserPlatform, the provided interface should not be closed. Otherwise, it throws ArgumentError.

In kReleaseMode or non widget testing process, suInterface will not accept MockSuperuser and throw IllegalInstanceError instead. Therefore, ensure all dummy data are removed already.

Implementation

static void bindInstance(SuperuserInterface? suInterface) {
  if (!Platform.isWindows && !Platform.isMacOS && !Platform.isLinux) {
    throw UnsupportedError("Unknown platform");
  }

  if (kReleaseMode &&
      !Platform.environment.containsKey("FLUTTER_TEST") &&
      suInterface is MockSuperuser) {
    throw IllegalInstanceError(
        "Mock instance cannot be used in release mode.");
  }

  flushInstance();

  late SuperuserInterface newInst;

  if (suInterface == null) {
    // Denote null interface as uses default implementation.
    if (Platform.isWindows) {
      newInst = WindowsSuperuser();
    } else if (Platform.isMacOS || Platform.isLinux) {
      newInst = UnixSuperuser();
    }
  } else {
    if (suInterface is SuperuserPlatform && suInterface.isClosed) {
      throw ArgumentError(
          "The provided interface should not be closed already.",
          "suInterface");
    }

    newInst = suInterface;
  }

  _instance = newInst;
}