谢谢!
带有来自Firestore的streambuilders的Flutter +飞镖。
我有一个包含多个流构建器的页面,一切正常,但是当我在其上添加一个列表视图(以滚动显示所有流构建器和文本小部件)时,flutter会出现错误。有解决这个问题的方法吗?
如果我删除listView,则该代码有效,但不能“滚动”。
Widget mainScreen() {
double screenHeight = MediaQuery.of(context).size.height;
double screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Center(
child: Text(
"DEALS",
style: TextStyle(fontSize: 40, color: Colors.white),
),
),
),
body: SafeArea(
child: Container(
color: Colors.grey[900],
child: Column(
children: <Widget>[
TypeButton(
onPressed: () {
goToLaunch();
},
colour: Colors.white,
text: 'MAKE DEAL',
width: screenWidth / 1.1,
height: screenHeight / 14,
fontSize: 20,
),
SizedBox(
height: screenHeight / 40,
),
Container(
height: screenHeight / 2,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: <Widget>[
Flex(
direction: Axis.vertical,
children: <Widget>[
Text(
"Today",
style: TextStyle(fontSize: 30, color: Colors.white),
textAlign: TextAlign.left,
),
DealList(
widget: widget,
day: 'Monday',
),
Text(
"Tomorrow",
style: TextStyle(fontSize: 30, color: Colors.white),
textAlign: TextAlign.left,
),
DealList(
widget: widget,
day: 'Monday',
),
Text(
"Wednesday",
style: TextStyle(fontSize: 30, color: Colors.white),
textAlign: TextAlign.left,
),
DealList(
widget: widget,
day: 'Monday',
),
],
),
],
),
),
],
),
),
),
);
}
class DealList extends StatelessWidget {
DealList({this.widget, this.day});
final PubDeals widget;
final String day;
@override
Widget build(BuildContext context) {
double screenHeight = MediaQuery.of(context).size.height;
double screenWidth = MediaQuery.of(context).size.width;
return Flexible(
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('${widget.currentPub.pubPlace}deals')
.where("pubID", isEqualTo: widget.currentUser.pubID)
.where("on${day}Night", isEqualTo: true)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text('Loading...');
default:
return new ListView(
scrollDirection: Axis.horizontal,
children:
snapshot.data.documents.map((DocumentSnapshot document) {
return Padding(
padding: EdgeInsets.all(10),
child: Material(
borderRadius: BorderRadius.circular(10),
color: document['typeBeer'] == true
? Color.fromRGBO(181, 148, 13, 100)
: document['typeDE'] == true
? Color.fromRGBO(4, 149, 63, 100)
: Color.fromRGBO(155, 14, 6, 100),
child: Container(
width: screenWidth / 3,
height: screenHeight / 7,
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(
document['description'],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 15,
),
),
Text(
'uur',
style: TextStyle(
color: Colors.white,
fontSize: 15,
),
),
Align(
alignment: FractionalOffset.bottomLeft,
child: Text(
'€ ${document['price'].toString()}',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20),
),
),
],
),
),
),
),
);
}).toList(),
);
}
},
),
);
}
}
这是我得到的错误:
flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performLayout():
flutter: RenderFlex children have non-zero flex but incoming height constraints are unbounded.
flutter: When a column is in a parent that does not provide a finite height constraint, for example if it is
flutter: in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
flutter: flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
flutter: space in the vertical direction.
flutter: These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
flutter: cannot simultaneously expand to fit its parent.
flutter: Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
flutter: children (using Flexible rather than Expanded). This will allow the flexible children to size
flutter: themselves to less than the infinite remaining space they would otherwise be forced to take, and
flutter: then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
flutter: constraints provided by the parent.
flutter: If this message did not help you determine the problem, consider using debugDumpRenderTree():
flutter: https://flutter.dev/debugging/#rendering-layer
flutter: http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
flutter: The affected RenderFlex is:
flutter: RenderFlex#af67b relayoutBoundary=up16 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Flex ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ← ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#cf643] ← ⋯, parentData: <none> (can use size), constraints: BoxConstraints(w=414.0, 0.0<=h<=Infinity), size: MISSING, direction: vertical, mainAxisAlignment: start, mainAxisSize: max, crossAxisAlignment: center, verticalDirection: down)
flutter: The creator information is set to:
flutter: Flex ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ←
flutter: KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ←
flutter: ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#cf643] ← ⋯
flutter: See also: https://flutter.dev/layout/
flutter: If none of the above helps enough to fix this problem, please don't hesitate to file a bug:
flutter: https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter: