gpt4 book ai didi

dart - 如果嵌套在子 : [] 中,则不会呈现 StreamBuilder 小部件

转载 作者:IT王子 更新时间:2023-10-29 06:46:18 29 4
gpt4 key购买 nike

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

我遇到了这个问题,我需要使 StreamBuilder 成为 Widget Array 的一部分。如果 StreamBuilder 小部件是小部件的单个子部件,则它可以正常呈现。但如果是子小部件数组的一部分,它就不起作用。例如

下面是我使用 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,
)

上面的代码可以正常工作,因为 Container 只有一个“子”元素。

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

 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.

如果有人能提供帮助,我将不胜感激,我只是找不到 Google 来帮助解决这个问题!!

最佳答案

我认为您缺少对错误和数据尚不可用的检查,如中所示

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)');
}
},
)

关于dart - 如果嵌套在子 : <Widget>[] 中,则不会呈现 StreamBuilder 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792605/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com