【问题标题】:How do I listen to a Document with Auth UID being its reference?如何收听以 Auth UID 为参考的文档?
【发布时间】:2019-04-14 10:31:27
【问题描述】:

在我的搜索中,有一些方法可以做到这一点。一种方法是同时使用FutureBuilderStreamBuilder。但我想避免在 FutureBuilder 中嵌套 StreamBuilder?

我尝试创建一个方法并从流生成器中调用它。

    _getData() async{

  FirebaseUser user =  await FirebaseAuth.instance.currentUser();
String uid = user.uid.toString();
var snapshots = 
Firestore.instance.collection('userinfo').document(uid).snapshots();
      return snapshots;
}


@override
 Widget build(BuildContext context) {
return new Scaffold(
  appBar: AppBar(
    title: Text('Retrieve Text Input'),
  ),
  body: new Container(
    padding: EdgeInsets.only(top: 20.0, left: 10.0, right: 10.0),
    child:  new StreamBuilder(
        stream: _getData(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var userDocument = snapshot.data;
             return new Column(
                    children: <Widget>[
                      TextFormField(
                        initialValue:  userDocument["name"].toString(),
                        //controller: _AdContr,
                        decoration: new InputDecoration(
                          labelText: 'Name',
                          fillColor: Colors.white,
                          border: new OutlineInputBorder(
                            borderRadius: new BorderRadius.circular(25.0),
                            borderSide: new BorderSide(),
                          ),
                          //fillColor: Colors.green
                        ),
                      ),
                      SizedBox(height: 20),
                      TextFormField(
                        //initialValue: Text(userDocument["Surname"]).toString(),
                        //controller: _AdContr,
                        decoration: new InputDecoration(
                          labelText: 'Surname',
                          fillColor: Colors.white,
                          border: new OutlineInputBorder(
                            borderRadius: new BorderRadius.circular(25.0),
                            borderSide: new BorderSide(),
                          ),
                          //fillColor: Colors.green
                        ),
                      )
                    ],
                  );
          }
        }),
   ),
 );
}

但我收到此错误:

类型“未来”不是类型“流”的子类型

我该如何解决这个问题?

【问题讨论】:

    标签: dart flutter google-cloud-firestore


    【解决方案1】:

    您将需要StreamBuilder 嵌套在FutureBuilder 中,因为您需要等待来自 FirebaseAuth 的用户才能获取流。不过,这还不错
    这就是 Flutter 的工作原理:嵌套小部件。

    ..., child: FutureBuilder(
            future: FirebaseAuth.instance.currentUser(),
            builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
              if (snapshot.connectionState != ConnectionState.done) return Container();
              return StreamBuilder<DocumentSnapshot>(
                stream: Firestore.instance.collection('userinfo').document(snapshot.data.uid).snapshots(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (!snapshot.hasData) return Container();
                  return Column(...);
                },
              );
            })
    

    另一个对其工作至关重要的方面是您总是从任何构建器返回一个小部件。如果您不将小部件返回给 Flutter 中的构建器,您的应用将中断,即会引发运行时异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      相关资源
      最近更新 更多