locate static method

FlutterSdk? locate(
  1. String projectRoot, {
  2. String? configured,
  3. Map<String, String>? environment,
})

The SDK for projectRoot, trying in order: configured (a setting), the FLUTTER_ROOT environment variable, the SDK that last ran pub (flutterRoot in package_config.json), and flutter on the PATH where it is a link into an SDK. Null when none is found. It never starts a program; find also asks a launcher outside an SDK (a snap's, a version manager's shim), and locate then reuses its answer. Setting FLUTTER_ROOT to the SDK's folder always works.

Implementation

static FlutterSdk? locate(
  String projectRoot, {
  String? configured,
  Map<String, String>? environment,
}) {
  final env = environment ?? Platform.environment;
  final candidates = [
    configured,
    env['FLUTTER_ROOT'],
    PackageConfig.read(projectRoot)?.flutterRoot,
    _fromPath(env),
    for (final l in _launchers(env).take(1)) _asked[l],
  ];
  for (final c in candidates) {
    if (c == null || c.isEmpty) continue;
    final sdk = FlutterSdk(c);
    if (sdk.exists) return sdk;
  }
  return null;
}