Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

flutter - StreamProvider not rebuilding the widget tree when AsyncValue updates

I am to listening firebase AuthStateChanges stream and provide the stream with streamProvider to change the view based on the stream value. And I did this:

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Stream get currentUser => _auth.authStateChanges();
}

final userStream = StreamProvider.autoDispose((ref) => AuthService().currentUser);

class AuthenticationWrapper extends ConsumerWidget {
  @override
  Widget build(BuildContext context, ScopedReader watch) {
    final user = watch(userStream);
    print('AutenticationWrapper build method got called');
    return user.when(
      data: (data) {
        if (data?.uid == null) {
          print('I am currently Logged out ??');
          return LogInPage();
        } else {
          print('I am logged in user??');
          return HomePage();
        }
      },
      loading: () => CircularProgressIndicator(),
      error: (e, s) => Text('Oops'),
    );
  }
}

I was expecting to have LogIn page to be rendered when the AsyncValue of the streamProvider gets changed. The above code didn't work as expected; in fact, it prints the message but it doesn't return the Widget it is supposed to return. However, when I hot restart the app it will render the correct Widget based on the stream value.

Why doesn't the build method re-render when this: final user = watch(userStream); receives an update?

question from:https://stackoverflow.com/questions/65830274/streamprovider-not-rebuilding-the-widget-tree-when-asyncvalue-updates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you should probably watch(userStream.stream) to be notified when the stream itself updates. Haven't played much with StreamProvider though, so I could be wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...