如何修复ListView生成的错误?
伙计们,我有个问题要问你们: 我已经建立了一个页面视图。在这个页面视图中是一个列表视图,在这个列表视图中是一个小部件列表。 但不幸的是,由于某些原因,我的代码无法工作。。。我不知道为什么。我还是个新手。我非常感谢你的帮助。 生成错误的代码如下所示:如何修复ListView生成的错误?,listview,user-interface,flutter,dart,flutter-pageview,Listview,User Interface,Flutter,Dart,Flutter Pageview,伙计们,我有个问题要问你们: 我已经建立了一个页面视图。在这个页面视图中是一个列表视图,在这个列表视图中是一个小部件列表。 但不幸的是,由于某些原因,我的代码无法工作。。。我不知道为什么。我还是个新手。我非常感谢你的帮助。 生成错误的代码如下所示: Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: ListView(
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: ListView(
children: createHomePage(
controller: _controller,
content: widget.content,
title: widget.title,
menuButtonAction: widget.menuButton,
showShadow: widget.showShadow,
context: context),
),
);
}
}
如果你能帮助我,我将非常感激。当我用列替换ListView时,代码运行良好,但不幸的是,我无法滚动。。。但它必须是可滚动的!
我不断得到以下错误:
I/flutter ( 4539): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4539): The following assertion was thrown building NotificationListener<KeepAliveNotification>:
I/flutter ( 4539): Incorrect use of ParentDataWidget.
I/flutter ( 4539): Expanded widgets must be placed inside Flex widgets.
I/flutter ( 4539): Expanded(no depth, flex: 1, dirty) has no Flex ancestor at all.
I/flutter ( 4539): The ownership chain for the parent of the offending Expanded was:
I/flutter ( 4539): RepaintBoundary ← IndexedSemantics ←
NotificationListener<KeepAliveNotification> ← KeepAlive ←
I/flutter ( 4539): AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ← ShrinkWrappingViewport
I/flutter ( 4539): ← ⋯
I/flutter ( 4539):
I/flutter ( 4539): The relevant error-causing widget was:
I/flutter ( 4539): ListView
I/颤振(4539):══╡ WIDGETS库捕获到异常╞═══════════════════════════════════════════════════════════
I/flatter(4539):在构建NotificationListener时抛出以下断言:
I/flatter(4539):不正确使用ParentDataWidget。
I/flatter(4539):扩展的小部件必须放在Flex小部件中。
I/颤振(4539):扩展(无深度,弯曲:1,脏)根本没有弯曲祖先。
I/Flatter(4539):违规扩展的父对象的所有权链为:
I/颤振(4539):重新绘制边界← 指数符号学←
通知侦听器← 保持活力←
I/颤振(4539):自动激活← 键子树← 银表← MediaQuery← 填缝料← 收缩包装可视端口
I/颤振(4539):← ⋯
I/颤振(4539):
I/颤振(4539):导致错误的相关小部件是:
I/颤振(4539):列表视图
我如何解决这个问题?我试了很多。例如,我将小部件列表放在一列中,并将ListView更改为SingleChildListView,但这也没有帮助。但是,我收到了另一条错误消息。这是:
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4539): The following assertion was thrown during performLayout():
I/flutter ( 4539): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter ( 4539): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter ( 4539): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter ( 4539): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
I/flutter ( 4539): space in the vertical direction.
I/flutter ( 4539): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter ( 4539): cannot simultaneously expand to fit its parent.
I/flutter ( 4539): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
I/flutter ( 4539): children (using Flexible rather than Expanded). This will allow the flexible children to size
I/flutter ( 4539): themselves to less than the infinite remaining space they would otherwise be forced to take, and
I/flutter ( 4539): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
I/flutter ( 4539): constraints provided by the parent.
I/flutter ( 4539): If this message did not help you determine the problem, consider using debugDumpRenderTree():
I/flutter ( 4539): https://flutter.dev/debugging/#rendering-layer
I/flutter ( 4539): http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
I/flutter ( 4539): The affected RenderFlex is:
I/flutter ( 4539): RenderFlex#bfee3 relayoutBoundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Column ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ← ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#665ae] ← ⋯, parentData: <none> (can use size), constraints: BoxConstraints(w=411.4, 0.0<=h<=Infinity), size: MISSING, direction: vertical, mainAxisAlignment: start, mainAxisSize: max, crossAxisAlignment: center, verticalDirection: down)
I/flutter ( 4539): The creator information is set to:
I/flutter ( 4539): Column ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ←
I/flutter ( 4539): KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ←
I/flutter ( 4539): ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#665ae] ← ⋯
I/flutter ( 4539): See also: https://flutter.dev/layout/
I/flutter ( 4539): If none of the above helps enough to fix this problem, please don't hesitate to file a bug:
I/flutter ( 4539): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 4539):
I/flutter ( 4539): The relevant error-causing widget was:
I/flutter ( 4539): Column
══╡ 呈现库捕获到异常╞═════════════════════════════════════════════════════════
I/flatter(4539):在performLayout()期间抛出了以下断言:
I/颤振(4539):RenderFlex子项具有非零的弯曲,但传入的高度约束是无限制的。
I/颤振(4539):当柱位于不提供有限高度约束的父项中时,例如,如果它是
I/flatter(4539):在垂直滚动中,它将尝试沿垂直轴收缩包裹其子对象。设定
I/颤振(4539):子对象上的弯曲(例如,使用展开)表示子对象将展开以填充剩余部分
I/颤振(4539):垂直方向上的空间。
I/flatter(4539):这两个指令是相互排斥的。如果父对象要收缩包装其子对象,则子对象
I/颤振(4539):无法同时展开以适合其父对象。
I/Frutter(4539):考虑将MaxAxIsSead设置为MainAxisSize.min,并使用FLUFFIT。
I/颤振(4539):儿童(使用灵活而非扩展)。这将允许灵活的孩子调整尺寸
I/flatter(4539):他们自己的空间小于他们将被迫占据的无限剩余空间,并且
I/flatter(4539):然后将导致RenderFlex收缩包裹子对象,而不是展开以适应最大值
I/颤振(4539):由父级提供的约束。
I/Frutter(4539):如果此消息不能帮助您确定问题,请考虑使用Debug gDimePrimeReTee():
I/颤振(4539):https://flutter.dev/debugging/#rendering-层
I/颤振(4539):http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
I/颤振(4539):受影响的RenderFlex为:
I/flatter(4539):RenderFlex#bfee3 relayboundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(创建者:列)← 重新绘制边界← 指数符号学← 通知侦听器← 保持活力← 自动开机← 键子树← 银表← MediaQuery← 填缝料← 收缩包装可视端口← IgnorePointer-[GlobalKey#665ae]← ⋯, parentData:(可以使用大小),约束:BoxConstraints(w=411.4,0.0)
我现在还看不到完整的代码,但这对您来说意味着,您的listview需要一种高度,例如,如果您将其包装在一个容器中,则会给出这种高度
您可以做的第二件事是将shrinkwrap:true
放在listview中
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
margin: EdgeInsets.all(32),
decoration: BoxDecoration(border: Border.all(color: Colors.red)),
child: ListView(
shrinkWrap: true,
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
],
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(App());
类应用程序扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(),
正文:中(
子:容器(
保证金:全部(32),
装饰:框装饰(边框:border.all(颜色:Colors.red)),
子:ListView(
收缩膜:对,
儿童:[
ListTile(标题:文本(“项目1”),
ListTile(标题:文本(‘项目2’),
ListTile(标题:文本(“第3项”),
],
),
),
),
),
);
}
}
然后,listview只获取listview本身中所有小部件或项目的高度
希望有帮助。在listview中使用扩展的小部件是无效的,因为扩展的小部件没有任何可扩展的高度,所以删除扩展的小部件可以解决您的问题。创建主页广告。尝试使用扩展的小部件包装listview,使用列小部件包装扩展。首先,感谢您的帮助和回答。我很抱歉真的很高兴有人站出来。但不幸的是,这不起作用。我不知道为什么,但我仍然得到相同的错误。如果我用列替换ListView,一切都正常。容器、扩展或列帮助都没有。shrikWrap也没有帮助……你有其他想法吗?添加createHomePage的代码,我建议谢谢你的回答。还有你的ef
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
margin: EdgeInsets.all(32),
decoration: BoxDecoration(border: Border.all(color: Colors.red)),
child: ListView(
shrinkWrap: true,
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
],
),
),
),
),
);
}
}