【问题标题】:Could not find the correct Provider<T> above this BlocConsumer<T, S> Widget在此 BlocConsumer<T, S> 小部件上方找不到正确的 Provider<T>
【发布时间】:2021-09-20 11:53:14
【问题描述】:

首先,我阅读了几乎所有与此错误相关的问题,但找不到任何问题的答案。

我正在尝试使用 BlocProvider 在 SignInPage 中提供带有 BlocProvider 的子小部件:

class SignInPage extends StatelessWidget {
  const SignInPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Sign In'),
      ),
      body: BlocProvider(
        create: (context) => getIt<SignInFormBloc>(),
        child: const SignInForm(),
      ),
    );
  }
}

SignInForm 是:

class SignInForm extends StatelessWidget {
  const SignInForm({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<SignInFormBloc, SignInFormState>(
      listener: (context, state) {
        
      },
      builder: (context, state) {
        
      },
    );
  }
}

据我所知,BlocConsumer 应该在父小部件中搜索 SignInFormBloc,而父小部件是 BlocProvider,我为它提供了 SignInFormBloc 我不知道为什么它找不到它

我尝试将它包装在 Builder(builder: (context) {} ) 中,以防它在错误的上下文中搜索但也不起作用。

【问题讨论】:

  • 看来问题出在getIt部分。
  • 用 BlocBuilder 包起来就可以了

标签: flutter flutter-bloc


【解决方案1】:

我在 AppRouter 中犯了一个错误,使用 SignInPage 我使用了 SignInForm

@MaterialAutoRouter(

  routes: [
    AutoRoute(page: SplashPage, initial: false),
    // It should be SignInPage -_-
    AutoRoute(page: SignInForm, initial: true),
  ],
)
class $AppRouter {}

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 2021-07-16
    • 1970-01-01
    • 2021-08-24
    • 2021-10-27
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多