text property

  1. @override
String? get text
override

Get the text content of the response

Implementation

@override
String? get text {
  final candidates = _rawResponse['candidates'] as List?;
  if (candidates == null || candidates.isEmpty) return null;

  final content = candidates.first['content'] as Map<String, dynamic>?;
  if (content == null) return null;

  final parts = content['parts'] as List?;
  if (parts == null || parts.isEmpty) return null;

  // According to Google API docs, only return non-thinking content
  // Thinking content has thought: true flag, regular content has thought: false or no thought field
  final textParts = parts
      .where((part) {
        final isThought = part['thought'] as bool? ?? false;
        final text = part['text'] as String?;
        return !isThought && text != null && text.isNotEmpty;
      })
      .map((part) => part['text'] as String)
      .toList();

  return textParts.isEmpty ? null : textParts.join('\n');
}