0

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
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
moanrisy
  • 109
  • 1
  • 12

1 Answers1

1

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)
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • `class Socketio { StreamController chatController = StreamController(); Stream streamChat; ` And I have this for constructor `Socketio() { initSocket(); streamChat = chatController.stream.asBroadcastStream(); }` this is why Socketio class. I want to have StreamController from SocketIO class with object type class – moanrisy May 20 '19 at 05:02
  • Yes, but in your `main` file, you are not exposing `Socketio` directly, You are exposing `Sockerio.streamChat` through `StreamProvider`. – Rémi Rousselet May 20 '19 at 05:08
  • I want to use StreamProvider not provider – moanrisy May 20 '19 at 05:09
  • Like I said, replace `.of` with `.of` and it will work. You don't need the stream builder when using `StreamProvider`. – Rémi Rousselet May 20 '19 at 05:10
  • Chat class doesn't have streamChat field (it give me error like this if im using `.of` I want using stream builder so if I got new response from socket io, the chat automatically rebuild – moanrisy May 20 '19 at 05:12
  • You don't need to access `streamChat`. That's already taken care of. I'm the maintainer of the package you're using, and I can assure you that you want to use `.of`. – Rémi Rousselet May 20 '19 at 05:15
  • Would you give me example of using StreamProvider? You say to not use stream builder but in the example of the project, that using StreamBuilder https://github.com/rrousselGit/provider/blob/master/example/provider.dart – moanrisy May 20 '19 at 05:24
  • Thank you so much, I finally understand what do you mean. So I just need to use `newText(Provider.of(context).sender` then I can just `chatController.add(newChat())` in the SocketIo class to update the value. – moanrisy May 20 '19 at 05:36