0

在我使用提供者 getx 之前,我新建了一个 bloc/cubit 模式,我想问一些问题。

这个应用程序有近 40 页,我只想使用 bloc 10 页并在最后一页关闭时关闭 bloc 实例。

示例页面 1,我初始化 bloc,它在其他页面(2、3、4...9)上初始化将使用相同的 bloc/cubit 实例和 10 页关闭 bloc 实例关闭

这是路由器类和cubit在这里初始化

    class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final RegisterCubit _registerCubit = RegisterCubit();
    switch (settings.name) {
      case welcomeRoute:
        return MaterialPageRoute(builder: (_) => WelcomePage());
      case loginRoute:
        return MaterialPageRoute(builder: (_) => LoginPage());
      case homeRoute:
        return MaterialPageRoute(builder: (_) => HomePage());
      case referanceCodePreview:
        return MaterialPageRoute(
            builder: (_) => BlocProvider.value(
                  value: _registerCubit,
                  child: ReferancaCodePreviewView(),
                ));
      case registerOne:
        return MaterialPageRoute(
            builder: (_) => BlocProvider.value(
                  value: _registerCubit,
                  child: RegisterPageOne(),
                ));
      case registerTwo:
        return MaterialPageRoute(
            builder: (_) => BlocProvider.value(
                  value: _registerCubit,
                  child: RegisterPageOTwo(),
                ));
      case registerThree:
        return MaterialPageRoute(
            builder: (_) => BlocProvider.value(
                  value: _registerCubit,
                  child: RegisterPageOThree(),
                ));
      case registerFour:
        return MaterialPageRoute(
            builder: (_) => BlocProvider.value(
                  value: _registerCubit,
                  child: RegisterPageFour(),
                ));
      case registerFive:
        return MaterialPageRoute(
            builder: (a) => BlocProvider.value(
                  value: _registerCubit,
                  child: RegisterPageFive(),
                ));

本页四和本页不听_registerCubit

class RegisterPageFour extends StatefulWidget   {
  RegisterPageFour({Key? key}) : super(key: key);

  @override
  State<RegisterPageFour> createState() => _RegisterPageFourState();
}

class _RegisterPageFourState extends State<RegisterPageFour> with
ImageSelectPicker {
  @override
  Widget build(BuildContext context) {
    return  BlocConsumer<RegisterCubit, RegisterState>(
        listener: (contex, state) {},
        builder: (context, state) {
          return buildScaffold(context, state);
        },
      );
  }

导航页四

 CustomButton(
            buttonWidth: MediaQuery.of(context).size.width,
            onPress: () => Navigator.pushNamed(context, "/registerFour"),
            title: "İleri"),

你能帮我解决这个问题吗?

4

1 回答 1

0
final RegisterCubit _registerCubit = RegisterCubit();

我建议您在generateRoute函数之外创建 RegisterCubit 对象。像这样的东西,

class Router {
 final RegisterCubit _registerCubit = RegisterCubit();
 static Route<dynamic> generateRoute(RouteSettings settings) {
 switch (settings.name) {
 }
于 2022-01-07T05:59:07.900 回答