如果嵌套在子项中,则不会呈现StreamBuilder窗口小部件:<widget> []

时间:2018-06-11 07:38:36

标签: dart google-cloud-firestore flutter

我正在尝试从Firebase的FireStore数据库中读取数据,并在flutter应用中显示为列表。

我遇到过这个问题,我需要在Widget Array中创建 StreamBuilder 部分。如果 StreamBuilder 小部件作为小部件的单个子级,则呈现正常。但是,如果是子窗口小部件数组的一部分,它就无法工作。 e.g。

下面是我使用 StreamBuilder

的Widget
Widget postsListWidget = new StreamBuilder(
        stream: Firestore.instance.collection('baby').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return const Text('Loading...');
          return new ListView.builder(
            itemCount: snapshot.data.documents.length,
            padding: const EdgeInsets.only(top: 10.0),
            itemExtent: 55.0,
            itemBuilder: (context, index) =>
                _buildListItem(context, snapshot.data.documents[index]),
          );
        });

这是我使用StreamBuilder小部件与单个子父级,例如容器

return new Scaffold(
        body: new Container(
      child: postsListWidget,
    )

上述代码可以正常使用,因为容器只有一个孩子&#39;元件。

但是,如果我使用另一个可以有多个孩子的小部件,例如行, StreamBuilder 将不会呈现。 e.g。

 return new Scaffold(
        body: new Row(
      children: <Widget>[postsListWidget],
    )

以上代码不会呈现StreamBuilder小部件。

我在调试窗口中出现以下错误

flutter: Another exception was thrown: RenderBox was not laid out: RenderViewport#9162d NEEDS-LAYOUT NEEDS-PAINT
flutter: Another exception was thrown: RenderBox was not laid out: RenderViewport#9162d NEEDS-PAINT
flutter: Another exception was thrown: Tried to paint a RenderObject reentrantly.
flutter: Another exception was thrown: 'package:flutter/src/widgets/text.dart': Failed assertion: line 213 pos 15: 'data != null': is not true.
flutter: Another exception was thrown: Tried to paint a RenderObject reentrantly.

如果有人可以提供帮助,我会感激不尽,我无法找到谷歌来帮忙解决这个问题!

1 个答案:

答案 0 :(得分:0)

我认为您缺少对错误的检查和尚未提供的数据,如

所示

https://docs.flutter.io/flutter/widgets/StreamBuilder-class.html

new StreamBuilder<int>(
  stream: _lot?.bids, // a Stream<int> or null
  builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
    if (snapshot.hasError)
      return new Text('Error: ${snapshot.error}');
    switch (snapshot.connectionState) {
      case ConnectionState.none: return new Text('Select lot');
      case ConnectionState.waiting: return new Text('Awaiting bids...');
      case ConnectionState.active: return new Text('\$${snapshot.data}');
      case ConnectionState.done: return new Text('\$${snapshot.data} (closed)');
    }
  },
)