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.

suInterface can only accept MockSuperuser if kDebugMode or it performs widget testing. Using mock interface in kReleaseMode or kProfileMode causes IllegalInstanceError throw.

Implementation

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

  if (!kUnderDevelop && 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;
}