Flutter Block:无法在多页状态表单中以第二屏幕访问更新的状态字段
我需要帮助。
我正在创建一个多页面形式,该表单被分为两个屏幕。
- 第一个屏幕
- 第二屏幕
两个屏幕共享相同的bloc,即Formbloc,and ComeState的状态相同。当我们在第一个屏幕中单击“提交”按钮时,它使用state.copywith()方法更新状态变量,然后将我们带到第二个屏幕。我正在使用navigator.pushnamed(上下文,'/screen2');移至第二屏幕。
将BLOC从Apploute类提供到第二屏幕:
static final _formBloc = FormBloc();
Following code is called during Navigation:
// Route to second screen
static BlocProvider<FormBloc> secondScreenRoute() {
return BlocProvider.value(value: _formBloc, child: const SecondScreen());
}
但是在第二个屏幕中,使用state.copywith()方法更新的所有字段都将恢复到其初始状态。
例如: 在第一个屏幕中,有用于用户名的输入FormTextField。当用户输入他的名字时,我使用state.copywithmethod()更新状态 假设用户输入名称“ John”
emit(state.copyWith(
name: John,
status: Formz.validate([state.name, name]),
));
,当我使用state.name在第二个屏幕中访问它时,然后我得到空字符串”。
我不明白为什么会发生这种情况。我不是在创建新的BLOC实例,并且我已经将已创建的BLOC实例传递给了第二屏幕,但随后使用state.copywith()方法更新的所有属性都将在第二屏幕上恢复为其初始值。
你能帮忙吗?
I need help.
I am creating a multi page form which is divided into 2 screens.
- First Screen
- Second Screen
Both screen share same bloc which is FormBloc, and same state which is FormState. When we click submit button in first screen, it update the state variables using state.copyWith() method and then it take us to 2nd screen. I am using Navigator.pushNamed(context, '/screen2'); to move to second screen.
Bloc is provided to second screen from the AppRoute class:
static final _formBloc = FormBloc();
Following code is called during Navigation:
// Route to second screen
static BlocProvider<FormBloc> secondScreenRoute() {
return BlocProvider.value(value: _formBloc, child: const SecondScreen());
}
But In the second screen all the fields which was updated using state.copyWith() method gets restored to their initial state.
For Example:
In first Screen there is input formtextfield for user name. When user enters his name, I update state using state.copywithMethod()
Suppose user enter name "John" then
emit(state.copyWith(
name: John,
status: Formz.validate([state.name, name]),
));
And When I am accessing this in second screen using state.name then I am getting empty string "".
I fail to understand why this is happening. I am not creating new bloc instance and I am passing already created bloc instance to second screen but then also all the properties which was updated using state.copyWith() method gets restored to their initial value on second screen.
Could you please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试更好而不是使用blocprovider.value,在材料应用中全局创建bloc,如下所示:
这将允许在启动应用程序时已经创建它,因此您以后占用的所有blocs都将在该应用程序中具有值状态。
现在,只需尝试进行正常的导航,请删除您创建的另一个BlocProvider以及。值。
You can try better instead of using BlocProvider.value, create the Bloc globally in your MaterialApp as follows :
This will allow to have it already created when you start your application, and thus all the Blocs that you occupy later will have the value in the state.
Now just try to do a normal navigation, remove the other BlocProvider you have created as well as the .value.