In flutter we make use of the StreamBuilder with receives a Stream e gives us a Snapshot object that contains "data" but also contains "error" objects.
I want to make a function with async* that yields data but due to some conditions it can yield also some errors. How can I achieve that in Dart?
Stream<int> justAFunction() async* {
yield 0;
for (var i = 1; i < 11; ++i) {
await Future.delayed(millis(500));
yield i;
}
yield AnyError(); <- I WANT TO YIELD THIS!
}
And then, in the StreamBuilder:
StreamBuilder(
stream: justAFunction(),
builder: (BuildContext context, AsyncSnapshot<RequestResult> snapshot) {
return Center(child: Text("The error tha came: ${snapshot.error}")); <- THIS SHOULD BE THE AnyError ABOVE!
},
)