stream method Null safety
Sends a prompt and returns a Stream of text chunks (SSE streaming).
This is the Dart equivalent of C++ NocLLM's beginStream() + loop().
Instead of an event-loop pattern, Dart uses native async Streams.
await for (final chunk in ai.stream('Tell me a fable')) {
stdout.write(chunk);
}
You can also use .listen() for callback-style handling:
ai.stream('Tell me a fable').listen(
(chunk) => stdout.write(chunk),
onDone: () => print('\n[DONE]'),
onError: (e) => print('Error: $e'),
);
Implementation
Stream<String> stream(String prompt, {bool addToHistory = true}) {
if (addToHistory) {
_conversationHistory.add({'role': 'user', 'content': prompt});
}
return _performStream(prompt);
}