locate static method
The SDK for projectRoot, trying in order: configured (a setting);
when the project pins a Flutter version (pinOf), an SDK of exactly
that version (installed); then 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 — whatever their version. 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;
if (configured != null && configured.isNotEmpty) {
final sdk = FlutterSdk(configured);
if (sdk.exists) return sdk;
}
if (pinOf(projectRoot) case final pin?) {
final exact = installed(pin, projectRoot: projectRoot, environment: env);
if (exact != null) return exact;
}
final candidates = [
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;
}