stream builder is not updating after navigation

二次信任 提交于 2021-02-10 12:43:56

问题


I am using a stream builder to detect if user logged in or not.

 return StreamBuilder<User>(
        stream: AuthService().user,
        builder: (context, snapshot) {
          if (snapshot.hasData)
            return SectionWrapper();
          else
            return Authentication();
        });

and this is the stream I am using

 Stream<User> get user {
    return _auth.onAuthStateChanged.map(_userFromFirebaseUser);
  }
  //create user object based on firebase user
  User _userFromFirebaseUser(FirebaseUser user) {
    return user != null ? User(uid: user.uid, email: user.email) : null;
  }

in the section wrapper, there are 2 buttons that navigates to two different sections of the app, when I use sign out method in one of these sections, the stream builder is not updating, and it requires refresh to update the state.

also i tried to put a button in section wrapper to sign out, it works and update UI before I navigate to one of the sections.

and this is the sectionWrapper() widget tree.

Column(
        children: <Widget>[
          CupertinoButton(
              child: Text('Donation & Selling Section'),
              onPressed: () {
                Navigator.pushReplacementNamed(
                    context, Section1.routeName,
                    arguments: user);
              }),
          CupertinoButton(
              child: Text('Bookstores Section'),
              onPressed: () {
                Navigator.pushReplacementNamed(
                    context, Section2.routeName);
              }),
          
          //works before navigation, does not work after navigation back here
          CupertinoButton(
              child: Text('Sign out'),
              onPressed: () async {
                await AuthService().signOut();
              }),
        ],
      ),

I also tried to use stream provider with consumer and I ended with the same problem.

   class Wrapper extends StatelessWidget {
  static const String routeName = '/';

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    return user == null ? Authentication() : SectionWrapper();
  }
}

I wrapped material app with a stream provider.

MultiProvider(
      providers: [
        StreamProvider<User>.value(value: AuthService().user),
//other providers
      ],
      child: MaterialApp(

回答1:


Your conditional Is not being tested in the sub page. You should call Provider.of in the sectionWrapper() page.

You’re saying:

  • if user is logged in, build sectionWrapper widget
  • if user is not logged in build Authentication widget.

That is it for that build method. Once that is built, you need to say what you want to do next.

Eg. On the next screen, above your widget tree, set provider.of like you did. Then, if the value changes, provider will force a rebuild for that screen’s build method.



来源:https://stackoverflow.com/questions/63338282/stream-builder-is-not-updating-after-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!