Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Last active November 22, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miquelbeltran/38d259d6fbfa8891bfeb3b5c76033ddb to your computer and use it in GitHub Desktop.
Save miquelbeltran/38d259d6fbfa8891bfeb3b5c76033ddb to your computer and use it in GitHub Desktop.
Did you try to use stream.first?
Did you try returning "Top User is: $name"?
Did you try to use future.asStream()?
Check the classes Stream and Future for the method specifications.
Future<String> topUser(Stream<String> stream) async {
// implement this method
}
Stream<String> usernameAsStream(Future<String> username) {
// implement this method
}
Future<String> topUser(Stream<String> stream) async {
var user = await stream.first;
return 'Top User is: $user';
}
Stream<String> usernameAsStream(Future<String> username) {
return username.asStream();
}
void main() async {
try {
final stream = Stream.fromIterable(['First', 'Second']);
final value = await topUser(stream);
if (value != 'Top User is: First') {
_result(false,
['Something went wrong! The result of `topUser` is incorrect.']);
return;
}
} catch (error) {
_result(false, [
'Something went wrong! Tried calling `firstEvent` but got exception: $error'
]);
return;
}
try {
final value = await usernameAsStream(Future.value('First')).single;
if (value != 'First') {
_result(false, [
'Something went wrong! The result of `usernameAsStream` is incorrect.'
]);
return;
}
} catch (error) {
_result(false, [
'Something went wrong! Tried calling `usernameAsStream` but got exception: $error'
]);
return;
}
_result(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment