Jamilur Rahman
Jamilur Rahman

Reputation: 69

Flutter Stream Builder - Stream Getting null

I would like to get data from both streams before building widgets. The first stream provides uid for the database path. I can not zip this both stream as I would need to get uid first to build the second data.

I tried using nested streams but keep getting error

The method '[]' was called on null. Receiver: null Tried calling:

my code is given. How can I solve this?

class _DiabeticaState extends State<Diabetica> {

 @override
 Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(
          "Diabetica",
          style: TextStyle(
              color: Colors.black, fontWeight: FontWeight.w400, fontSize: 22),
        ),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.monetization_on),
            onPressed: () {},
          ),
        ],
      ),
      body: StreamBuilder(
        stream: FirebaseAuth.instance.onAuthStateChanged,
        builder: (BuildContext context, user) {
          if (user.hasData) {
            String name = "";
            String photoUri = "";
            String u = "";
            if (user.connectionState == ConnectionState.active) {
              name = user.data.displayName;
              photoUri = user.data.photoUrl;
              u = user.data.uid;

              return StreamBuilder(
                stream: Firestore.instance.document('users/$u').snapshots(),
                builder: (BuildContext context, db) {
                  if (db.hasData) {
                    double age = 0;
                    double height = 0;
                    double weight = 0;
                    double a1c = 0;
                    double bmr = 0;
                    if (db.connectionState == ConnectionState.active) {
                        age = db.data['age'];
                        height = db.data['height'];
                        weight = db.data['weight'];
                        a1c = db.data['a1c'];
                        bmr = db.data['bmr'];

                      return Container();
                    }
                    return Container(

                    );
                  }
                  return new LoadingScreen();
                },
              );
            }
            return new LoadingScreen();
          }
          return new LoginAlter();
        },
      ),
    );
   }

 }

Upvotes: 1

Views: 3051

Answers (1)

E.Benedos
E.Benedos

Reputation: 1767

In your case using this CombineLatestStream.combine2 isn't the correct way due to your implementation. Have you tried to set initialData property on the second StreamBuilder?

For an example try to look here: StreamBuilder initialData

Upvotes: 1

Related Questions