Flutter:如何在控制器中触发集体事件?
总而言之,我希望“ context.read()。add(enabledevent())”可以从控制器本身或可以用来触发事件的其他替代方案中工作并更新状态:
class SplashCtrl extends GetxController {
@override
void onReady() {
User user = loadUser();
if (user.isExampleEnabled) {
context.read<ExampleBloc>().add(EnabledEvent());
}
}
我尝试过的另一件事是:
ExampleBloc testBloc = ExampleBloc();
testBloc.add(TestEvent());
在控制器中,它似乎确实触发了事件,但是UI不会随状态更改而更新。我似乎真的需要上下文才能改变。有什么想法吗?
更多详细信息和上下文:
我想不仅在页面本身中触发bloc事件,还要在页面的控制器中触发事件!
因此,我的flutter应用程序中有页面,每个视图都有这样的控制器设置:
class SplashPage extends GetView<SplashCtrl> {
@override
Widget build(BuildContext context) {
Get.lazyPut(() => SplashCtrl());
return Scaffold(...);
}
}
SplashCtrl是此页面的控制器。此Splash视图页面是加载其运行功能时(基本上是立即)运行功能的第一批页面之一,例如检查用户是否已登录并将其数据加载到应用程序中,例如:
class SplashCtrl extends GetxController {
@override
void onReady() {
// run stuff here
}
我已经能够摆脱在页面中创建lambda函数,并根据其所做的工作触发事件并这样的切换:
IconSwitchedButtonUi(
value: state.isExampleOn,
icon: Images.toggleIcon,
title: "Is Example enabled?",
onChanged: (value) {
if (value) {
context.read<ExampleBloc>().add(EnabledEvent());
} else {
context.read<ExampleBloc>().add(DisabledEvent());
}
},
),
但是现在,如果用户对其一个字段具有一定的值,则我需要一个集团事件来触发和更改状态。我该怎么做?如何从控制器内触发事件? “ context.read”不起作用,因为控制器没有上下文,还是这样做?
In summary I want the "context.read().add(EnabledEvent())" to work from the controller itself or some other alternative I can use to trigger an event from the controller and update the state:
class SplashCtrl extends GetxController {
@override
void onReady() {
User user = loadUser();
if (user.isExampleEnabled) {
context.read<ExampleBloc>().add(EnabledEvent());
}
}
Another thing I tried was this:
ExampleBloc testBloc = ExampleBloc();
testBloc.add(TestEvent());
in the controller, and it does seem to trigger the event, but the UI doesn't update with the state change. I seem to really need the Context for that to change. Any ideas?
MORE DETAILS AND CONTEXT:
I want to trigger bloc events not just in the page itself, but in the page's controller!
So I have pages in my flutter app with each view having a controller set up like this:
class SplashPage extends GetView<SplashCtrl> {
@override
Widget build(BuildContext context) {
Get.lazyPut(() => SplashCtrl());
return Scaffold(...);
}
}
SplashCtrl is the controller for this page. This splash view page is one of the first pages that loads it runs functions when it's added and ready (basically instantly), such as checking if the user is logged in and loading their data to the app, like this:
class SplashCtrl extends GetxController {
@override
void onReady() {
// run stuff here
}
I have been able to get away with creating lambda functions in the pages and triggering events based on what they do and toggle like this:
IconSwitchedButtonUi(
value: state.isExampleOn,
icon: Images.toggleIcon,
title: "Is Example enabled?",
onChanged: (value) {
if (value) {
context.read<ExampleBloc>().add(EnabledEvent());
} else {
context.read<ExampleBloc>().add(DisabledEvent());
}
},
),
but now I need a bloc event to trigger and change the state a bit if a user has a certain value for one of their fields. How do I do that? How do I trigger an event from within the controller? The "context.read" doesn't work because the controller doesn't have context, or does it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不起作用的,因为您声明了全新的bloc实例,这些实例没有由任何
blocprovider
与小部件树连接,因此,您没有办法可以触发这个 bloc事件,它会散发出您的blocbuilder
可能会做出反应。由于没有向小部件树提供集团read()
找不到此集团。为了进行这项工作,您需要:
通过
buildContext
,其具有BlocProvider提供的bloc或cubit
..或直接在控制器内部收听BLOC更改,
请记住,即使在这种方法中,给出
splashcrtl
构造函数的BLOC实例必须与blocprovider
提供的相同。This does not work because you were declaring totally new instance of bloc that wasnt connected by any
BlocProvider
to your widget tree, hence there is no way you could trigger this bloc events that would emit state yourBlocBuilder
could react to. Since there is no bloc provided to widget treeread()
wont find this bloc.In order for this work you need to:
Pass
BuildContext
that have some Bloc or Cubit provided byBlocProvider
..or listen to bloc changes directly inside controller
Remember that even in this approach bloc instance given to
SplashCrtl
constructor must be the same that is provided byBlocProvider
to a widget tree in order for this to work.我能够通过在控制器中添加后期的buildContext来解决此问题:
在此之后,在页面本身中,我通过了这样的上下文:
然后我能够在控制器本身中使用上下文!
I was able to resolve this by adding a late BuildContext to my controller:
After that, in the page itself, I passed the context like this:
Then I was able to use the context within the controller itself!!