【问题标题】:StreamBuilder<Event>(dirty, state: _StreamBuilderBaseState<Event, AsyncSnapshot<Event>>#ac213):StreamBuilder<Event>(脏,状态:_StreamBuilderBaseState<Event, AsyncSnapshot<Event>>#ac213):
【发布时间】:2021-04-27 20:48:58
【问题描述】:

我只是在学习颤振,我发现了问题。我无法从 firebase 检索数据,以前我可以使用相同的代码检索数据,但现在我不能。我发现我发现了一个错误,如下所示。 那是怎么发生的?而在此之前它不是。我希望有人能帮助我

这是我的代码

``` StreamBuilder(
        stream: dbRef.child("Data").onValue,
        builder: (context, snapshot) {
          if (snapshot.hasData &&
              !snapshot.hasError &&
              snapshot.data.snapshot.value != null) {
            return Column(
              children: <Widget>[
                Container(
                    height: size.height * 1,
                    child: Stack(
                      children: <Widget>[
                        Positioned(
                            height: 40,
                            bottom: 520,
                            left: 0,
                            right: 0,
                            child: Container(
                              margin: EdgeInsets.symmetric(
                                  horizontal: kDefaultPadding),
                              padding: EdgeInsets.symmetric(
                                  horizontal: kDefaultPadding),
                              height: 10,
                              decoration: BoxDecoration(
                                color: Colors.cyan,
                                borderRadius: BorderRadius.circular(15),
                                boxShadow: [
                                  BoxShadow(
                                    offset: Offset(0, 10),
                                    blurRadius: 50,
                                    color: kPrimaryColor.withOpacity(0.5),
                                  ),
                                ],
                              ),
                              child: Center(
                                child: Text("PLANT MOISTURE CONTROLLER",
                                    style: TextStyle(fontSize: 18)),
                              ),
                            )),],))],)}}) ```

这是错误:

【问题讨论】:

  • 在没有数据的时候也需要返回一些东西。当 (!snapshot.hasData) 时返回一些东西

标签: firebase flutter firebase-realtime-database


【解决方案1】:

您需要在数据加载时返回一些小部件,例如:

StreamBuilder(
  stream: dbRef.child("Data").onValue,
  builder: (context, snapshot) {
    if (!snapshot.hasData)
      return Center(child: CircularProgressIndicator());
    
    return Container(child: Text('real content'));
  });  

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2020-11-13
    • 2012-07-29
    • 2021-08-03
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多