当我想从Firebase打印数据时,构建器会重复很多次

时间:2019-05-04 11:20:37

标签: firebase dart flutter google-cloud-firestore

我对颤振和火力有问题。

我想在包含表单的flutter界面中从firebase打印数据,数据成功打印,但是当我想在文本字段中写加载案例时不要让我,所有构建器都会重复很多次很快。

打印效果很好 加载表格后加载表格后 而且我不会写,所以如何解决这个问题?

 @override
  Widget build(BuildContext context) {
return new Scaffold(
    appBar: AppBar(
      title: Text("Profile"),
      elevation: 1.5,
    ),
    body: StreamBuilder<DocumentSnapshot>(
      stream: Firestore.instance
          .collection('users')
          .document(widget.user.uid)
          .snapshots(),
      builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        }
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Text('Loading.. '); 
            break;
          default:
            return checkRole(snapshot.data);
            break;
        }
      },
    ));
  }

2 个答案:

答案 0 :(得分:0)

使用FutureBuilder,因为您尝试更改文档,

FutureBuilder(future: Firestore.instance.collection('users').document(widget.user.uid).get(),
            builder:(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){

      if(snapshot.hasData){
          return Text('${snapshot.data.data}');
      }

    return Text('Loading.. '); 

              });

答案 1 :(得分:-1)

我通过添加snapshot.hasdata来解决这个问题

builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.connectionState == ConnectionState.done ||
            snapshot.hasData) {
          return checkRole(snapshot.data);
        } else {
          return new Center(child: new Text('loading'));
        }