text property

String? text

The text content of the text parts of the first of candidates, if any.

If the prompt was blocked, or the first candidate was finished for a reason of FinishReason.recitation or FinishReason.safety, accessing this text will throw a GenerativeAIException.

If the first candidate's content contains any text parts, this value is the concatenation of the text.

If there are no candidates, or if the first candidate does not contain any text parts, this value is null.

If there is more than one candidate, all but the first are ignored. See Candidate.text to get the text content of candidates other than the first.

Implementation

String? get text => switch (candidates) {
      [] => switch (promptFeedback) {
          PromptFeedback(
            :final blockReason,
            :final blockReasonMessage,
          ) =>
            // TODO: Add a specific subtype for this exception?
            throw GenerativeAIException('Response was blocked'
                '${blockReason != null ? ' due to $blockReason' : ''}'
                '${blockReasonMessage != null ? ': $blockReasonMessage' : ''}'),
          _ => null,
        },
      [final candidate, ...] => candidate.text,
    };