ensureFlutterProject function
Implementation
Future<void> ensureFlutterProject(String projectDir) async {
final pubspecFile = File('$projectDir/pubspec.yaml');
if (!await pubspecFile.exists()) {
throw 'No pubspec.yaml found. Must be run from a Flutter project root.';
}
final content = await pubspecFile.readAsString();
final yaml = loadYaml(content) as Map;
// Check if flutter is a dependency
final dependencies = yaml['dependencies'];
if (dependencies == null || !dependencies.containsKey('flutter')) {
throw 'This tool only works with Flutter projects. Add Flutter as a dependency first.';
}
// Check if flutter_localizations is a dependency
final hasLocalizations = dependencies['flutter_localizations'] != null;
if (!hasLocalizations) {
throw '''
Missing required dependency: flutter_localizations
Add the following to your pubspec.yaml:
dependencies:
flutter_localizations:
sdk: flutter
intl:
''';
}
// Check if intl is a dependency
final hasIntl = dependencies.containsKey('intl');
if (!hasIntl) {
throw '''
Missing required dependency: intl
Add the following to your pubspec.yaml:
dependencies:
intl:
''';
}
}