Flutter Stream Builder-流获取空

时间:2019-05-14 19:19:48

标签: flutter

在构建窗口小部件之前,我想从两个流中获取数据。第一个流为数据库路径提供uid。我无法同时压缩这两个流,因为我需要先获取uid才能构建第二个数据。

我尝试使用嵌套流,但是不断出错

方法'[]'在null上被调用。 接收者:null 尝试致电:

给出了我的代码。我该怎么解决?

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

 }

1 个答案:

答案 0 :(得分:0)

由于您的实施方式,因此您使用此CombineLatestStream.combine2的方法不正确。您是否尝试在第二个StreamBuilder上设置initialData属性?

例如,请尝试在此处查看:StreamBuilder initialData