使用StreamBuilder时动态加载嵌套的Listview-已经侦听了错误的状态

时间:2020-04-15 05:06:04

标签: flutter

我当前正在使用带有嵌套列表视图的水平页面视图。我已经链接了一个API,以通过广播的流加载数据。

嵌套视图用于位置的水平列表,列表视图连接到该位置。

我已经看到一些描述使用广播的答案,尽管这似乎不适用于List视图重新渲染卡片的情况。

问题

  1. 在水平滚动时卸载列表视图的数据时,streamBuilder将不会重新加载数据。显示“状态错误:流已被收听。”

页面浏览

Container(
         width: screenWidth,
             child: Column(
             mainAxisSize: MainAxisSize.min,
             children: <Widget>[
                Text(locations[index], //Currently this is a static list of locations
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 35,
                        fontFamily: 'OpenSans',
                        fontWeight: FontWeight.w600,
                        height: 1)),
                Expanded( //This expanded was used to work around the errors of using a listview in a column. 
                  child: StreamBuilder(
                    stream: manager.eventListView,
                     builder: (BuildContext context,
                         AsyncSnapshot<List<eventModel>> snapshot) {
                            switch (snapshot.connectionState) {
                            case ConnectionState.none:
                            case ConnectionState.waiting:
                            case ConnectionState.active:
                              return Center(child: CircularProgressIndicator());
                              break;
                            case ConnectionState.done:
                              return displayCard(context, index, snapshot);
                              break;
                            default:
                              return (Text("Error" +
                                  (snapshot.connectionState).toString()));
                              break;
                          }
                        },
                      ),
                    ),
                  ],
                ));
          })

显示卡方法


 return ListView.separated(
      physics: eventScrollPhysics(),
      controller: _scrollController,
      itemCount: events?.length-1 ?? 0,
      itemBuilder: (context, index) {
        eventModel _event = events[index];


        return Container(
          height: 140,
          width: screenWidth - 5,
          child: Card(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(25.0)),
            elevation: 6,
            child: Stack(
              children: <Widget>[
                Positioned(
                  top: 15,
                  left: 30,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(_event.title,
                          style: TextStyle(
                              fontFamily: 'OpenSans',
                              fontWeight: FontWeight.w600,
                              fontSize: 20)),
                      Text(_event.date),
                    ],
                  ),
                ),
                Positioned(
                    top: 15,
                    right: 30,
                    child: Icon(Icons.account_circle, size: 50)),
                Positioned(
                    top: 70,
                    child: new SizedBox(
                      height: 10.0,
                      child: new Center(
                        child: new Container(
                          margin: new EdgeInsetsDirectional.only(
                              start: 1.0, end: 1.0),
                          height: 5.0,
                          color: Colors.red,
                        ),
                      ),
                    )),
                Positioned(top: 85, left: 30, child: Text(_event.description)),
                Positioned(
                  bottom: 30,
                  right: 3,
                  child: Column(children: <Widget>[
                    ButtonTheme(
                      minWidth: 10.0,
                      height: 20.0,
                      buttonColor: Color.fromRGBO(250, 205, 96, 75),
                      shape: RoundedRectangleBorder(
                        borderRadius: new BorderRadius.circular(18.0),
                      ),
                      child: RaisedButton(
                        onPressed: () {
                          // TODO: Button Action for Joining event
                        },
                        child: Text("Join"),
                      ),
                    ),
                    Text("10 People are going",
                        style: TextStyle(height: 0.1, fontSize: 12)),
                  ]),
                ),
              ],
            ),
          ),
        );
      },

...... Separator Function

0 个答案:

没有答案