generate method

  1. @override
Future<void> generate(
  1. ProjectContext context
)
override

Executes the plugin's code-generation and configuration logic.

Implementation

@override
Future<void> generate(ProjectContext context) async {
  final pubspecPath = '${context.projectPath}/pubspec.yaml';

  // 1. Add SharedPreferences dependency
  Services.yamlService.addDependency(pubspecPath, 'shared_preferences', '^2.2.2');

  // 2. Render service template
  await Services.templateService.renderTemplate(
    templateSubPath: 'services/shared_preferences_service.dart',
    targetPath: '${context.projectPath}/lib/core/services/shared_preferences_service.dart',
    replacements: {},
  );

  // 3. Register initialization logic in main.dart
  final currentImports = context.templateReplacements['STATE_MANAGEMENT_IMPORTS'] ?? '';
  context.templateReplacements['STATE_MANAGEMENT_IMPORTS'] =
      "${currentImports}import 'core/services/shared_preferences_service.dart';\n";

  final currentInit = context.templateReplacements['STATE_MANAGEMENT_INIT'] ?? '';
  context.templateReplacements['STATE_MANAGEMENT_INIT'] =
      "$currentInit\n  await SharedPreferencesService().init();";

  // Set replacements for features to read/write onboarding progress
  context.templateReplacements['PREFS_IMPORT'] =
      "import '${context.coreServicesRelativePath}shared_preferences_service.dart';\n";
  context.templateReplacements['PREFS_CHECK'] =
      "final completed = SharedPreferencesService().getBool('has_completed_onboarding') ?? false;";
  context.templateReplacements['PREFS_SAVE'] =
      "await SharedPreferencesService().setBool('has_completed_onboarding', true);";
}