blocprovider.of()拨打不包含寄存器cubit的上下文

发布于 2025-02-04 16:47:45 字数 1325 浏览 4 评论 0原文

当我尝试创建一个新用户时,出现了错误,注册页面昨天工作了,我没有更改任何内容。你能帮助我吗?

这是我的代码

注册库

    class RegisterCubit extends Cubit<RegisterStates> {
      RegisterCubit() : super(RegisterInitialState());
      static RegisterCubit get(context) => BlocProvider.of(context);
      bool isHidePassword = true;
      IconData password = Icons.visibility_outlined;
    
      void changePassword() {
        isHidePassword = !isHidePassword;
        password = isHidePassword
            ? Icons.visibility_outlined
            : Icons.visibility_off_outlined;
        emit(ChangePassVisibility());
      }
    ...

注册页面

 class Register extends StatelessWidget {
      final formKey = GlobalKey<FormState>();
      final emailController = TextEditingController();
      final passwordController = TextEditingController();
    
  Register({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (context) => RegisterCubit(),
        child: BlocConsumer<RegisterCubit, RegisterStates>(
          listener: (context, state) {},
          builder: (context, state) {
            var cubit = RegisterCubit.get(context);
            return Scaffold(
              ...

the error show up when I try to create a new user, registr page worked yesterday and I have not changed anything on it. Can you help me?

Here is my code

register cubit

    class RegisterCubit extends Cubit<RegisterStates> {
      RegisterCubit() : super(RegisterInitialState());
      static RegisterCubit get(context) => BlocProvider.of(context);
      bool isHidePassword = true;
      IconData password = Icons.visibility_outlined;
    
      void changePassword() {
        isHidePassword = !isHidePassword;
        password = isHidePassword
            ? Icons.visibility_outlined
            : Icons.visibility_off_outlined;
        emit(ChangePassVisibility());
      }
    ...

register page

 class Register extends StatelessWidget {
      final formKey = GlobalKey<FormState>();
      final emailController = TextEditingController();
      final passwordController = TextEditingController();
    
  Register({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (context) => RegisterCubit(),
        child: BlocConsumer<RegisterCubit, RegisterStates>(
          listener: (context, state) {},
          builder: (context, state) {
            var cubit = RegisterCubit.get(context);
            return Scaffold(
              ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

够钟 2025-02-11 16:47:45

BlocProvider必须比消费者更高。您的BlocProvider和BlocConsumer都在同一上下文中,消费者无法以这种方式找到寄存器。您可以从应用程序的根小部件提供肘。因此,它将用于其余的构建环境。除此之外,您只需将bloconsumer用构建器窗口小部件包裹起来,它将提供一个可以访问您寄存器库的新的buildContext。

BlocProvider must be in a higher BuildContext than the Consumers. Your BlocProvider and BlocConsumer both are in the same context and consumer cannot find the RegisterCubit in that way. You can provide your cubit from root widget of the app. So it will be available for the rest of build contexts. Beside that you can just wrap your BlocConsumer with Builder widget, that will provide a new BuildContext that have access to the your RegisterCubit.

(り薆情海 2025-02-11 16:47:45

您可以尝试使用多BLOCPROVIDER包装“ Main.dart”的MiteralApp,然后注册您的Bloc提供商

示例:

Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
          create: (BuildContext context) => AppCubit(),
        ),
        BlocProvider(
          create: (BuildContext context) => RegisterCubit()
        ),
      ],
      child: BlocConsumer<AppCubit, AppStates>(
          listener: (context, state) {},
          builder: (BuildContext context, state) {
            return MaterialApp(

You can try wrapping your MaterialApp "in main.dart" with a MultiBlocProvider then register your bloc provider there

Example:

Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
          create: (BuildContext context) => AppCubit(),
        ),
        BlocProvider(
          create: (BuildContext context) => RegisterCubit()
        ),
      ],
      child: BlocConsumer<AppCubit, AppStates>(
          listener: (context, state) {},
          builder: (BuildContext context, state) {
            return MaterialApp(
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文