我正在尝试获取的清单 Cards,并尝试使用Expanded小部件,但出现了overflow错误
我的代码:
new Expanded(
child: StreamBuilder(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemCount: snapshot.data.documents.length,
padding: const EdgeInsets.only(top: 10.0),
itemExtent: 25.0,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data.documents[index];
return //Text(" ${ds['name']} ${ds['vote']}");
Card(
child: Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: const Icon(Icons.album),
title: const Text('The Enchanted Nightingale'),
subtitle: const Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
),
new ButtonTheme.bar( // make buttons use the appropriate styles for cards
child: ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('BUY TICKETS'),
onPressed: () { /* ... */ },
),
FlatButton(
child: const Text('LISTEN'),
onPressed: () { /* ... */ },
),
],
),
),
],
),
),
);
});
})),
Run Code Online (Sandbox Code Playgroud)
我得到的错误是: Incorrect use of ParentDataWidget.
完整错误:
正在执行热重装... I / flutter(9119):??? 小工具图书馆的例外情况???????????????????????????????????????????????? ??????????????? I / flutter(9119):构建了DefaultTextStyle(debugLabel:(englishLike I / flutter(9119):body1).merge(blackMountainView body1),merge(blackMountainView body1),继承:false,color:Color(0xdd000000),family:Roboto, I / Flutter(9119):大小:14.0,重量:400,基线:字母,装饰:TextDecoration.none,softWrap:包装I / Flutter(9119):以盒子宽度,溢出:夹):I / flutter(9119) :不正确使用ParentDataWidget。I / flutter(9119):扩展的窗口小部件必须直接放在Flex窗口小部件内。I / flutter(9119):Expanded(无深度,flex:1,脏)具有Flex祖先,但它们之间还有其他小部件:I / flutter(9119):这些小部件不能介于Expanded及其Flex之间。I / flutter(9119):违规Expanded的父级的所有权链为:I / flutter(9119):DefaultTextStyle?AnimatedDefaultTextStyle吗?_InkFeatures- [GlobalKey#93e52墨水渲染器]?I / flutter(9119):NotificationListener吗?CustomPaint?_ShapeBorderPaint?PhysicalShape I / flutter(9119):?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I / flutter(9119):违规Expanded的父级的所有权链为:I / flutter(9119):DefaultTextStyle?AnimatedDefaultTextStyle吗?_InkFeatures- [GlobalKey#93e52墨水渲染器]?I / flutter(9119):NotificationListener吗?CustomPaint?_ShapeBorderPaint?PhysicalShape I / flutter(9119):?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I / flutter(9119):违规Expanded的父级的所有权链为:I / flutter(9119):DefaultTextStyle?AnimatedDefaultTextStyle吗?_InkFeatures- [GlobalKey#93e52墨水渲染器]?I / flutter(9119):NotificationListener吗?CustomPaint?_ShapeBorderPaint?PhysicalShape I / flutter(9119):?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。DefaultTextStyle吗?AnimatedDefaultTextStyle吗?_InkFeatures- [GlobalKey#93e52墨水渲染器]?I / flutter(9119):NotificationListener吗?CustomPaint?_ShapeBorderPaint?PhysicalShape I / flutter(9119):?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。DefaultTextStyle吗?AnimatedDefaultTextStyle吗?_InkFeatures- [GlobalKey#93e52墨水渲染器]?I / flutter(9119):NotificationListener吗?CustomPaint?_ShapeBorderPaint?PhysicalShape I / flutter(9119):?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。?_MaterialInterior?材质?填充??I / flutter(9119):?????????????????????????????????????????????? ????????????????????????????????????????????????????????? ??????? I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。I /聊天(9119):uid = 10096(com.example.flutterapp)线程3相同的3行I / flutter(9119):引发了另一个异常:不正确使用ParentDataWidget。
更新
这是我得到的输出屏幕:
如果我删除了Expanded,输出将变成这样:
我想通了,整个问题是在使用itemExtent: 25.0,时ListView.builder通过移除它,一切都变得默认扩张和平稳运行。
在寻找解决方案时,我遇到了这个,这个和这个,这有助于我以更简洁的代码重建应用程序,下面是谁感兴趣的应用程序:
main.dart:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'BabyModel.dart';
import 'BabyCard.dart';
void main() => runApp(MyApp(
textInput: Text("Text Widget"),
));
class MyApp extends StatefulWidget {
final Widget textInput;
MyApp({this.textInput});
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
bool checkBoxValue = false;
@override
Widget build(BuildContext ctxt) {
return StreamBuilder(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (_, AsyncSnapshot<QuerySnapshot> snapshot) {
var documents = snapshot.data?.documents ?? [];
var baby =
documents.map((snapshot) => BabyData.from(snapshot)).toList();
return BabyPage(baby);
},
);
}
}
class BabyPage extends StatefulWidget {
final List<BabyData> allBaby;
BabyPage(this.allBaby);
@override
State<StatefulWidget> createState() {
return BabyPageState();
}
}
class BabyPageState extends State<BabyPage> {
@override
Widget build(BuildContext context) {
// var filteredBaby = widget.allFish.where((BabyData data) {
// data.name = 'Dana';
// }).toList();
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Container(
child: ListView.builder(
itemCount: widget.allBaby.length,
padding: const EdgeInsets.only(top: 10.0),
itemBuilder: (context, index) {
return BabyCard(widget.allBaby[index]);
})
),
)));
}
}
Run Code Online (Sandbox Code Playgroud)
BabyModel.dart:
import 'package:cloud_firestore/cloud_firestore.dart';
class BabyData {
final DocumentReference reference;
String name;
int vote;
BabyData.data(this.reference,
[this.name,
this.vote]) {
// Set these rather than using the default value because Firebase returns
// null if the value is not specified.
this.name ??= 'Frank';
this.vote ??= 7;
}
factory BabyData.from(DocumentSnapshot document) => BabyData.data(
document.reference,
document.data['name'],
document.data['vote']);
void save() {
reference.setData(toMap());
}
Map<String, dynamic> toMap() {
return {
'name': name,
'vote': vote,
};
}
}
Run Code Online (Sandbox Code Playgroud)
BabyCard.dart:
import 'package:flutter/material.dart';
import 'BabyModel.dart';
class BabyCard extends StatefulWidget {
final BabyData baby;
BabyCard(this.baby);
@override
State<StatefulWidget> createState() {
return BabyCardState(baby);
}
}
class BabyCardState extends State<BabyCard> {
BabyData baby;
String renderUrl;
BabyCardState(this.baby);
Widget get babyCard {
return
new Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: const Icon(Icons.album),
title: Text('The ${baby.name} is having:'),
subtitle: Text('${baby.vote} Votes.'),
),
new ButtonTheme.bar( // make buttons use the appropriate styles for cards
child: new ButtonBar(
children: <Widget>[
new FlatButton(
child: const Text('Thumb up'),
onPressed: () { /* ... */ },
),
new FlatButton(
child: const Text('Thumb down'),
onPressed: () { /* ... */ },
)]))]));
}
@override
Widget build(BuildContext context) {
return new Container(
child: babyCard,
);
}
}
Run Code Online (Sandbox Code Playgroud)
输出为:
| 归档时间: |
|
| 查看次数: |
6287 次 |
| 最近记录: |