#StackBounty: #flutter #flutter-provider StreamProvider returns no data when used with Navigator

Bounty: 50

The issue is that I don’t get any values out of my StreamProviders (which are defined on a global level) within my Authenticated route:

  runApp(MultiProvider(
    providers: [
      Provider.value(value: userService),
      StreamProvider.value(value: authService.userStream, initialData: null),
      StreamProvider.value(value: userService.userDataStream),
      StreamProvider.value(value: userService.characterStream),
    ],
    child: MyApp(),
  ));
}

I noticed that it’s to do with the logic that I have for my Navigator (if I remove it the provider values are passed down the widget tree as expected). The Navigator I’m using is based around the idea that the app has 3 states: Not Authenticated, Authenticated and Authenticated-First-Time. I get the value whether I’m authenticated from the loginStream (so far everything works):

@override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: loginStream,
      builder: (ctx, snapshot) {
        if (!snapshot.hasData) return Loading();
        LoginState state = snapshot.data;
        if (state == LoginState.LOGGED_OUT) return LoginScreen();
        if (state == LoginState.FIRST_TIME) return CharacterCreationScreen();
        return Navigator(
          key: navigatorKey,
          initialRoute: "/home",
          onGenerateRoute: (settings) => PageRouteBuilder(
            pageBuilder: (ctx, _, __) => routes(settings)(ctx),
            transitionsBuilder: pageTransition,
          ),
        );
      },
    );

The thing is that if I’m Authenticated and say in the HomeScreen, then both userDataStream and characterStream return null even if there’s actual data available. If I remove the StreamBuilder + LoginLogic itself and just have the Navigator widget returned above, then HomeScreen gets the correct values.

UPDATE:
I noticed that it’s not even the StreamBuilder. If I remove the 3 if’s within the builder, then the stream values are propagated correctly. Not sure why that happens.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.