Flutter Block:无法在多页状态表单中以第二屏幕访问更新的状态字段

发布于 2025-01-29 02:41:02 字数 1092 浏览 4 评论 0原文

我需要帮助。

我正在创建一个多页面形式,该表单被分为两个屏幕。

  1. 第一个屏幕
  2. 第二屏幕

两个屏幕共享相同的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.

  1. First Screen
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2025-02-05 02:41:02

您可以尝试更好而不是使用blocprovider.value,在材料应用中全局创建bloc,如下所示:

return 
    BlocProvider<FormBloc>( // like this
       create: (context) => FormBloc(),
       child: MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          initialRoute: PageNames.login,
          onGenerateRoute: RouteGenerator.generateRoute,
       ),
    ),
);

这将允许在启动应用程序时已经创建它,因此您以后占用的所有blocs都将在该应用程序中具有值状态。

现在,只需尝试进行正常的导航,请删除您创建的另一个BlocProvider以及。值。

You can try better instead of using BlocProvider.value, create the Bloc globally in your MaterialApp as follows :

return 
    BlocProvider<FormBloc>( // like this
       create: (context) => FormBloc(),
       child: MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          initialRoute: PageNames.login,
          onGenerateRoute: RouteGenerator.generateRoute,
       ),
    ),
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文