Cound not find the correct Provider above this consumer Widget

Issue

I’m trying use multiprovider from library Provider.
But it’s not working because it’s can’t find correct provider althought the upper widget before material is MultiProvider.

I Already try to import anything but it’s still not working

class _LoginViewState extends State<LoginView> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BaseView<LoginModel>(
      builder: (context, model, child) => Scaffold(
        backgroundColor: backgroundColor,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            StreamBuilder<Chat>(
              stream: Provider.of<Socketio>(context).streamChat,
              builder: (context, snapshot) {
                return Text(snapshot.data?.toString() ?? 'Foo');
              },
            ),
            LoginHeader(
              validationMessage: model.errorMessage,
              controller: _controller),
            model.state == ViewState.Busy
            ? CircularProgressIndicator()

that is where I canll Provider.of

void main() {
  setupLocator();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  MultiProvider(
      providers: [
        StreamProvider<User>(
          initialData: User.initial(),
          builder: (context) => locator<AuthenticationService>().userController,
        ),
        StreamProvider<Chat>(
          initialData: Chat.initial(),
          builder: (context) => locator<Socketio>().chatController,
        ),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(),
        initialRoute: '/login',
        onGenerateRoute: Router.generateRoute,
      ),
    );
  }
}```
this is where I provider socket io provider before Maetrialapp inside Multi Provider

Solution

The problem is you are doing:

Provider.of<Socketio>(context)

But you are never creating a provider of type Provider<Socketio>

Looking at your code, you likely want to use:

Provider.of<Chat>(context)

Answered By – Rémi Rousselet

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.