使用 Streambuilder 包装 CustomScrollView 和 Sliverlist 会抛出异常
Wrapping a CustomScrollView and a Sliverlist with a Streambuilder throws exception
我正在尝试使用 Streambuilder
从 Firebase firestore 监听实时文档,同时在 CustomScrollView
和 SliverList
中显示检索到的数据,但是当我这样做时列表中断,屏幕保持空白。
我尝试了 FutureBuilder
并且有效,但它不满足我的要求。
这是我为 streambuilder 编写的代码:
Widget sliverList() {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.transparent,
expandedHeight: 220.0,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: Column(
children: [Daily(), AddPost()],
),
),
),
StreamBuilder<QuerySnapshot>(
stream: feedServices.listenToFeedinStream(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
return SliverFixedExtentList(
itemExtent: 500,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (snapshot.hasData) {
Post post = Post.fromMap(snapshot.data.docs[index].data());
return Column(
children: [
FeedItem(
thread: post,
),
seperator()
],
);
} else if (!snapshot.hasData) {
return Text('no data');
} else if (snapshot.hasError) {
return Text('has err');
} else {
return Text('not specified');
}
},
childCount: snapshot.data.docs.length,
),
);
})
],
);
}
捕获异常:
flutter: The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
flutter: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#da6d3):
flutter: The getter 'docs' was called on null.
flutter: Receiver: null
flutter: Tried calling: docs
您应该只将 Sliver Widgets 传递给 slivers
列表。
StreamBuilder
不是 Sliver,所以你可以用 StreamBuilder
.
包裹整个 CustomScrollView
Widget sliverList() {
return StreamBuilder<QuerySnapshot>(
stream: feedServices.listenToFeedinStream(),
builder: (context, snapshot) {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.transparent,
expandedHeight: 220.0,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: Column(
children: [Daily(), AddPost()],
),
),
),
SliverFixedExtentList(
itemExtent: 500,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (snapshot.hasData) {
Post post = Post.fromMap(snapshot.data.docs[index].data());
return Column(
children: [
FeedItem(
thread: post,
),
seperator()
],
);
} else if (!snapshot.hasData) {
return Text('no data');
} else if (snapshot.hasError) {
return Text('has err');
} else {
return Text('not specified');
}
},
childCount: snapshot.data.docs.length,
),
)
],
);
},
);
}
我正在尝试使用 Streambuilder
从 Firebase firestore 监听实时文档,同时在 CustomScrollView
和 SliverList
中显示检索到的数据,但是当我这样做时列表中断,屏幕保持空白。
我尝试了 FutureBuilder
并且有效,但它不满足我的要求。
这是我为 streambuilder 编写的代码:
Widget sliverList() {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.transparent,
expandedHeight: 220.0,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: Column(
children: [Daily(), AddPost()],
),
),
),
StreamBuilder<QuerySnapshot>(
stream: feedServices.listenToFeedinStream(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
return SliverFixedExtentList(
itemExtent: 500,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (snapshot.hasData) {
Post post = Post.fromMap(snapshot.data.docs[index].data());
return Column(
children: [
FeedItem(
thread: post,
),
seperator()
],
);
} else if (!snapshot.hasData) {
return Text('no data');
} else if (snapshot.hasError) {
return Text('has err');
} else {
return Text('not specified');
}
},
childCount: snapshot.data.docs.length,
),
);
})
],
);
}
捕获异常:
flutter: The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
flutter: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#da6d3):
flutter: The getter 'docs' was called on null.
flutter: Receiver: null
flutter: Tried calling: docs
您应该只将 Sliver Widgets 传递给 slivers
列表。
StreamBuilder
不是 Sliver,所以你可以用 StreamBuilder
.
CustomScrollView
Widget sliverList() {
return StreamBuilder<QuerySnapshot>(
stream: feedServices.listenToFeedinStream(),
builder: (context, snapshot) {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.transparent,
expandedHeight: 220.0,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: Column(
children: [Daily(), AddPost()],
),
),
),
SliverFixedExtentList(
itemExtent: 500,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (snapshot.hasData) {
Post post = Post.fromMap(snapshot.data.docs[index].data());
return Column(
children: [
FeedItem(
thread: post,
),
seperator()
],
);
} else if (!snapshot.hasData) {
return Text('no data');
} else if (snapshot.hasError) {
return Text('has err');
} else {
return Text('not specified');
}
},
childCount: snapshot.data.docs.length,
),
)
],
);
},
);
}