被介绍给BLoC时,我创建了一个简单的类来更改bool
变量的值:
class SignInBloc {
StreamController<bool> _isLoading = StreamController<bool>();
Stream<bool> get getIsLoading => _isLoading.stream;
set setIsLoading(bool isLoading) => _isLoading.sink.add(isLoading); // Here is my problem (set)
void dispose(){
_isLoading.close();
}
}
当我使用set
关键字,然后在我的UI屏幕中调用它时:bloc.setIsLoading(false);
我得到一个例外:
Try correcting the name to the name of an existing method, or defining a method named 'setIsLoading'.
但是,当我set
在SignInBloc
课堂上取消该关键字时,它可以正常工作。我很困惑,不是最好使用这个关键字而不是直接声明我的二传手吗?而且,为什么我在起飞时没有收到错误消息?
Setter的使用要像它们是班级的公共领域一样。您只是在明确定义自己的setter。将您的预期值直接分配给设置器,如下所示:
bloc.setIsLoading = false;
使用的唯一好处set
是能够使用此语法。
起飞时set
,它已更改为常规方法,bloc.setIsLoading(false);
该语法将是正确的语法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句