1

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();
        },
      ),
    );
   }

 }
3
  • Yes i have.. Both the streams are different type i will uid to load first to get user data from firestore Commented May 15, 2019 at 5:59
  • I would need to get the uid before i flutter builds the widgets. Commented May 15, 2019 at 6:04
  • I am taking another approach. I am dividing the streams to the widgets where they are needed. If the data are null the widget visibility will be null Commented May 15, 2019 at 6:07

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.