autoFillWithAI static method

Future<void> autoFillWithAI({
  1. required Map<TextEditingController?, FieldType> fields,
  2. required AutoFillMode mode,
  3. required AiTextClient aiClient,
  4. Map<FieldType, String>? contextPerField,
})

AI-powered autofill:

  • fields: Map of controller -> fieldType ("name", "email", "bio", "product_description"...)
  • mode: faker / ai / hybrid
  • aiClient: pluggable AI client (OpenAiClient / MockAiClient / your own)
  • contextPerField: optional map to pass extra hints per field

Implementation

static Future<void> autoFillWithAI({
  required Map<TextEditingController?, FieldType> fields,
  required AutoFillMode mode,
  required AiTextClient aiClient,
  Map<FieldType, String>? contextPerField,
}) async {
  for (final entry in fields.entries) {
    final controller = entry.key;
    final fieldType = entry.value;

    if (controller == null) continue;

    try {
      String value;
      switch (mode) {
        case AutoFillMode.faker:
          value = _fakerValueFor(fieldType);
          break;
        case AutoFillMode.ai:
          value = await aiClient.generateFieldValue(
            fieldType: fieldType,
            context: contextPerField?[fieldType],
          );
          break;
        case AutoFillMode.hybrid:
          try {
            value = await aiClient.generateFieldValue(
              fieldType: fieldType,
              context: contextPerField?[fieldType],
            );
          } catch (_) {
            value = _fakerValueFor(fieldType);
          }
          break;
      }
      _setText(controller, value);
    } catch (_) {
      _setText(controller, _fakerValueFor(fieldType));
    }
  }
}