0

I'm using a StreamProvider to capture changes that occur in Firestore and display them on the user homepage. When the user initially signs in or registers, they're directed to this homepage and expect to see their details, for which I use the StreamProvider.

Keeping in mind that the first value fetched will be null, I've added a Loading Text Widget. However, the widget does not seem to change unless I hot-refresh the page and the Stateful Widget gets rebuilt again. What seems to be happening is that the Stateful Widget I'm using as my HomePage gets "built" only once due to which even when the stream gets populated with non-null values, the loading widget does not disappear because its parent widget doesn't get rebuilt.

This is the basic HomeScreen widget around which I'm wrapping my StreamProvider.

@Override
Widget build(BuildContext context) {
    return StreamProvider<UserDataModel>.value(
      initialData: null,
      value: DatabaseServices(userDetails: _userDetails).userDataDocument,
      updateShouldNotify: (previous, current) => true,
      child: Scaffold(
        backgroundColor: Colors.white,
        body: HomeBody(),
      )
    );
  }

This is the relevant snippet from the HomeBody widget

class HomeBody extends StatefulWidget {
  @override
  _HomeBodyState createState() => _HomeBodyState();
}

class _HomeBodyState extends State<HomeBody> {
  @override
  Widget build(BuildContext context) {
    final AuthenticationServices _authServices = AuthenticationServices();
    final UserDataModel _data = Provider.of<UserDataModel>(context);
    print(_data);
    print((_data != null) ? _data.name : 'NULL');
    /* Some code */
    Row(
      children: [
         Padding(
             padding: const EdgeInsets.fromLTRB(25, 0, 0, 5),
             child: Text(_data != null ? _data.name : 'LOADING...', style: dashboardName),
                ),
              ],
            ),
     Row(
       children: [
          Padding(
              padding: const EdgeInsets.fromLTRB(25, 2, 0, 5),
              child: Text(_data != null ? _data.scholarId : 'LOADING...', style: dashboardName),
                ),
              ],
            ),

I also noticed that the print statements I've used for debugging get executed only once printing those initial null values, until, I hot-refresh the page and the non-null values are shown.

Is there a way to go around this? I've been stuck for a couple of days now without making any significant advances and any sort of guidance would be appreciated.

Yash
  • 67
  • 5
  • Try to use `StreamBuilder` instead. More information about `StreamBuilder` [here](https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html). – kforjan May 27 '21 at 16:33
  • @kforjan Any reason in particular? I'm new to flutter so it'd be helpful if you could elaborate on how it happens to be better. – Yash May 27 '21 at 21:51
  • StreamProvider is used to provide content of a Stream to widgets without caring about reacting to events, while StreamBuilder is a widget that is used to rebuild it's children when Stream yields something new. In simpler words it means that provider just provides a stream to widgets, while builder changes UI depending on data conning from a stream – kforjan May 28 '21 at 01:05

0 Answers0