用于Firestore数据的StreamBuilder使用

时间:2020-07-16 14:57:01

标签: flutter google-cloud-firestore

我不确定我是否正确使用StreamBuilder,但有时它会以ConnectionState.active:为空到达snapshot.data.data。这就是为什么我检查并显示微调框是否为空的原因。我还放置了断点,但断点从未到达ConnectionState.done:

有时文档加载速度非常快,而有时ConnectionState.active中的微调器会在加载项目(especially after leaving the app open while the phone and then coming back and accessing an item)之前保留很长时间。有时它根本不加载。它只是显示微调器。

我觉得很奇怪的另一件事是,单击的第一项被立即加载,但是访问的第二或第三项却显示微调器很长时间。

  Stream<DocumentSnapshot> _itemStream;


  @override
  void initState() {
    super.initState();
    _itemStream = Firestore.instance
        .collection('content')
        .document(widget.item.documentID)
        .snapshots();
  }   

  _stream() {
      return StreamBuilder<DocumentSnapshot>(
        stream: _itemStream,
        builder:
            (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
          List<Widget> children;
          if (snapshot.hasError) {
            children = <Widget>[
              Icon(
                Icons.error_outline,
                color: Colors.red,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Error: ${snapshot.error}'),
              )
            ];
          } else {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                children = <Widget>[
                  SizedBox(
                    child: const CircularProgressIndicator(),
                    width: 60,
                    height: 60,
                  ),
                  const Padding(
                    padding: EdgeInsets.only(top: 16),
                    child: Text('Connection state none...'),
                  )
                ];
                break;
              case ConnectionState.waiting:
                children = <Widget>[
                  SizedBox(
                    child: const CircularProgressIndicator(),
                    width: 60,
                    height: 60,
                  ),
                  const Padding(
                    padding: EdgeInsets.only(top: 16),
                    child: Text('Connection state waiting...'),
                  )
                ];
                break;
              case ConnectionState.active:
                if (snapshot.data != null && snapshot.data.data != null) {
                  children = <Widget>[_itemBody(snapshot.data.data)];
                } else {
                  children = <Widget>[
                    SizedBox(
                      child: const CircularProgressIndicator(),
                      width: 60,
                      height: 60,
                    ),
                    const Padding(
                      padding: EdgeInsets.only(top: 16),
                      child: Text('Waiting for active...'),
                    )
                  ];
                }
                break;
              case ConnectionState.done:
                if (snapshot.data != null && snapshot.data.data != null) {
                  children = <Widget>[_itemBody(snapshot.data.data)];
                } else {
                  children = <Widget>[
                    SizedBox(
                      child: const CircularProgressIndicator(),
                      width: 60,
                      height: 60,
                    ),
                    const Padding(
                      padding: EdgeInsets.only(top: 16),
                      child: Text('Connection state done...'),
                    )
                  ];
                }
                break;
            }
          }

          return Column(
            children: children,
          );
        },
      );
    }

0 个答案:

没有答案