listview - 垂直视口被赋予无限宽度
问题描述
我是 Flutter 的新手,显然被一些我无法弄清楚的微不足道的事情困住了。
这是一个有状态的类:
class Menu extends StatefulWidget {
Menu({Key key}) : super(key: key);
@override
MenuState createState() => MenuState();
}
class MenuState extends State<Menu> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Padding(padding: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0)),
new FetchSummary(context: context),
],
),
);
}
}
在这个类中,我调用 FetchSummary。这个想法是从 JSON 文件中获取数据,该文件将在以后动态更改。
class FetchSummary extends StatefulWidget {
FetchSummary({Key key, this.context}) : super(key: key);
final BuildContext context;
@override
FetchSummaryState createState() => FetchSummaryState();
}
class FetchSummaryState extends State<FetchSummary> {
var _jsonFile = "assets/db-summaryData.json";
@override
Widget build(BuildContext context) {
return new FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(_jsonFile),
builder: (context, snapshot) {
var myData = json.decode(snapshot.data.toString());
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
} else {
//List<Post> yourPosts = snapshot.data.posts;
return Column(children: <Widget>[
Flexible(
child: ListView.builder(
//scrollDirection: Axis.vertical,
//shrinkWrap: true,
itemCount: myData.length,
padding: const EdgeInsets.only(bottom: 50),
itemBuilder: (BuildContext context, int index) {
return Container(
//margin: const EdgeInsets.symmetric(vertical: 1),
color: Color.fromRGBO(107, 164, 147, 0.5),
child: ListTile(
title: new Text(
"Dummy Text",
),
));
}))
]);
}
});
}
}
目前,在这个小部件中,我只是想在从 myData JSON 对象中获取任何内容之前发回一些文本。但是,当我尝试运行时,它开始抱怨如下:
The following assertion was thrown during performResize():
**Vertical viewport was given unbounded width.**
Viewports expand in the cross axis to fill their container and constrain their children to match their extent in the cross axis. In this case, a vertical viewport was given an unlimited amount of horizontal space in which to expand.
The relevant error-causing widget was:
ListView file:///D:/FlutterDev/Dashboard/dashboard/lib/main.dart:149:35
When the exception was thrown, this was the stack:
#0 RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:1191:15)
更新
如果我将 FutureBuilder 移动/复制到调用函数而不是作为一个类调用它,它可以正常工作。**
Widget build(BuildContext context) {
return new FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(_jsonFile),
builder: (context, snapshot) {
var myData = json.decode(snapshot.data.toString());
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
..............
最终更新
我从此替换了调用代码:
return new Container(
child: new Row(
children: <Widget>[
new Padding(padding: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0)),
new FetchSummary(context: context),
],
),
);
对此:
return new Container(
child:
new FetchSummary(context: context),
);
它奏效了。我认为它不喜欢 Row 小部件。
解决方案
尝试这个
return ListView.builder(
//scrollDirection: Axis.vertical,
//shrinkWrap: true,
itemCount: myData.length,
padding: const EdgeInsets.only(bottom: 50),
itemBuilder: (BuildContext context, int index) {
return Container(
//margin: const EdgeInsets.symmetric(vertical: 1),
color: Color.fromRGBO(107, 164, 147, 0.5),
child: ListTile(
title: new Text(
"Dummy Text",
),
));
});
推荐阅读
- pandas - SQLCMD--Using the :r Command I have a text file with multiple outputs,is there a way to add a delimiter between outputs or an easy way to split output
- list - 类中的函数列表 - Dart/Flutter
- google-kubernetes-engine - Why doesn't Istio on the GKE create a Gateway?
- eclipse - 运行 Prometheus 时,我的所有端点都开始返回 404
- dataframe - 在java中的spark数据框中选择groupBy中未包含列的对应值
- visual-studio-code - 在 VS-Code 编辑器中,如何区分自动换行和非自动换行?
- python - 使用正则表达式从 os.walk 给出的文件名中提取子字符串
- bash - 如何在bash中使用正则表达式从字符串中删除标点符号
- typescript - 将其他 AWS 资源添加到 CdkPipeline 应用程序阶段
- reactjs - 将自定义 Netlify CMS 小部件添加到 Gatsby starter Yellowcake