gpt4 book ai didi

dart - 使函数返回自定义流

转载 作者:行者123 更新时间:2023-12-03 03:27:24 26 4
gpt4 key购买 nike

是否可以创建一个返回自定义流并像这样处理的函数?

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

最佳答案

就像是:

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');

然后你可以像

关于dart - 使函数返回自定义流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30450135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com