使函数返回自定义流

时间:2015-05-26 05:27:04

标签: dart dart-async

是否可以创建一个返回自定义流并按此处理它的函数?

user.logIn('owner', '1234')
.listen(
  success (Object user) {
    print(user);
  },
  error: (Object user, Object error) {
    print(error);
  }
);

1 个答案:

答案 0 :(得分:2)

类似的东西:

class LoginResult {
  bool success = false;
  String username;
}

Stream<LoginResult> onLogin() async* {
  while(...) {
    yield new LoginResult()
      ..success = isSuccess
      ..userName = 'someUser';
  }
}

StreamController<LoginResult> onLoginController = new StreamController<LoginResult>();
// might not be necessary if you only need one listener at most
Stream<LoginResult> _onLogin = onLoginController.stream.asBroadcastStream(); 
Stream<LoginResult> get onLogin => _onLogin
...
onLoginController.add(new LoginResult()
  ..success = isSuccess
  ..userName = 'someUser');

然后你可以像

一样使用它