======== Exception caught by widgets library =======================================================
The following UnimplementedError was thrown building _SelectionKeepAlive(state: _SelectionKeepAliveState#35219):
UnimplementedError
The relevant error-causing widget was:
ListView ListView:file:///D:/Desktop/this%20is%20my%20homework/untitled13/lib/chat/message_page.dart:19:22
When the exception was thrown, this was the stack:
#0 MessageItem.createState (package:untitled13/chat/message_item.dart:71:5)
#1 new StatefulElement (package:flutter/src/widgets/framework.dart:5052:25)
#2 StatefulWidget.createElement (package:flutter/src/widgets/framework.dart:773:38)
... Normal element mounting (41 frames)
#43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#44 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18)
#45 SliverMultiBoxAdaptorElement.updateChild (package:flutter/src/widgets/sliver.dart:1451:37)
#46 SliverMultiBoxAdaptorElement.createChild. (package:flutter/src/widgets/sliver.dart:1436:20)
#47 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2682:19)
#48 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1428:12)
#49 RenderSliverMultiBoxAdaptor._createOrObtainChild. (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:353:23)
#50 RenderObject.invokeLayoutCallback. (package:flutter/src/rendering/object.dart:2300:59)
#51 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:1056:15)
#52 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:2300:14)
#53 RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:342:5)
#54 RenderSliverMultiBoxAdaptor.addInitialChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:428:5)
#55 RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:79:12)
#56 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#57 RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:149:12)
#58 RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:386:11)
#59 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#60 RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:550:13)
#61 RenderViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1634:12)
#62 RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1541:20)
#63 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#64 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#65 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#66 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#67 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#68 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#69 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#70 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#71 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#72 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#73 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#74 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#75 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#76 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#77 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#78 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#79 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#80 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#81 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#82 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#83 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#84 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#85 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#86 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#87 RenderCustomPaint.performLayout (package:flutter/src/rendering/custom_paint.dart:552:11)
#88 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#89 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#90 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#91 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#92 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#93 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:171:12)
#94 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:1080:7)
#95 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:240:7)
#96 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:410:14)
#97 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#98 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#99 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#100 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#101 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#102 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#103 _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1467:11)
#104 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#105 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#106 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:171:12)
#107 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:1080:7)
#108 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:240:7)
#109 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:410:14)
#110 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#111 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#112 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#113 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#114 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#115 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#116 _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1467:11)
#117 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#118 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#119 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#120 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#121 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#122 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#123 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#124 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#125 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#126 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#127 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#128 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#129 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#130 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#131 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#132 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#133 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#134 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#135 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#136 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#137 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#138 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#139 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#140 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#141 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#142 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#143 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#144 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#145 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#146 RenderOffstage.performLayout (package:flutter/src/rendering/proxy_box.dart:3813:14)
#147 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#148 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#149 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14)
#150 RenderObject.layout (package:flutter/src/rendering/object.dart:2189:7)
#151 RenderBox.layout (package:flutter/src/rendering/box.dart:2430:11)
#152 _RenderTheatre.performLayout (package:flutter/src/widgets/overlay.dart:856:15)
#153 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:2027:7)
#154 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1020:18)
#155 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:516:19)
#156 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:865:13)
#157 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:381:5)
#158 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1289:15)
#159 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1218:9)
#160 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1076:5)
#161 _invoke (dart:ui/hooks.dart:145:13)
#162 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#163 _drawFrame (dart:ui/hooks.dart:112:31)
====================================================================================================
======== Exception caught by widgets library =======================================================
The following assertion was thrown building NotificationListener:
'package:flutter/src/rendering/sliver_multi_box_adaptor.dart': Failed assertion: line 261 pos 16: 'child == null || indexOf(child) > index': is not true.
第一次遇到这个问题的时候我是很崩溃的,查过一些资料都没有很好的解决
问过chatgpt后得出一些原因,但是我不知道怎么改
这是chatgpt给出的解释:
这个错误提示是由Flutter引发的,它表明你的代码中存在未实现的功能。根据给出的堆栈跟踪信息,错误似乎发生在你的MessageItem
小部件的createState
方法中。下面是一些可能的解决方案:
检查你的MessageItem
类的代码,确保没有使用到未实现的功能或方法。如果有,你需要实现这些功能或使用其他可行的替代方法。
检查你使用的Flutter版本和相关库的版本。有时,升级到最新版本的Flutter和相关库可以修复一些已知的问题和错误。
检查你的代码是否引入了其他自定义小部件或库,这些可能导致冲突或未实现的功能。确保这些自定义小部件或库与你使用的Flutter版本兼容,并且没有任何未实现的功能。
这是之前报错的代码:
import 'package:flutter/material.dart';
import 'message.dart';
import 'package:date_format/date_format.dart';
import 'package:untitled13/common/touch_callback.dart';
class MessageItem extends StatefulWidget{
final MessageData message;
MessageItem(this.message);
@override
Widget build(BuildContext context){
return Container(
decoration: BoxDecoration(
color: Colors.white,
border:Border(bottom: BorderSide(width: 0.5,color: Color(0xFFd9d9d9))),
),
height: 64.0,
child: TouchCallBack(
onPressed: (){
},
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
margin: const EdgeInsets.only(left: 13.0,right: 13.0),
child: Image.network(message.avatar,width: 48.0,height: 48.0,),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
message.title,
style: TextStyle(fontSize: 16.0,color: Color(0xFF353535)),
maxLines: 1,
),
Padding(padding: const EdgeInsets.only(top: 8.0)),
Text(
message.subTitle,
style: TextStyle(fontSize: 14.0,color:Color (0xFFa9a9a9)),
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
],
)
),
Container(
alignment: AlignmentDirectional.topStart,
margin: const EdgeInsets.only(right: 12.0,top: 12.0),
child: Text(
formatDate(message.time, [HH,':',nn,':','ss']).toString(),
style: TextStyle(fontSize: 14.0,color: Color(0xff111111)),
),
)
],
),
),
);
}
@override
State createState() {
// TODO: implement createState
throw UnimplementedError();
}
}
这是修改后可以运行的代码:
import 'package:flutter/material.dart';
import 'message.dart';
import 'package:date_format/date_format.dart';
import 'package:untitled13/common/touch_callback.dart';
class MessageItem extends StatefulWidget {
final MessageData message;
MessageItem(this.message);
@override
MessageItemState createState() => MessageItemState();
}
class MessageItemState extends State {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border(bottom: BorderSide(width: 0.5, color: Color(0xFFd9d9d9))),
),
height: 64.0,
child: TouchCallBack(
onPressed: () {},
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
margin: const EdgeInsets.only(left: 13.0, right: 13.0),
child: Image.network(
widget.message.avatar,
width: 48.0,
height: 48.0,
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.message.title,
style: TextStyle(fontSize: 16.0, color: Color(0xFF353535)),
maxLines: 1,
),
Padding(padding: const EdgeInsets.only(top: 8.0)),
Text(
widget.message.subTitle,
style: TextStyle(fontSize: 14.0, color: Color(0xFFa9a9a9)),
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
],
),
),
Container(
alignment: AlignmentDirectional.topStart,
margin: const EdgeInsets.only(right: 12.0, top: 12.0),
child: Text(
formatDate(widget.message.time, [HH, ':', nn, ':', ss]).toString(),
style: TextStyle(fontSize: 14.0, color: Color(0xff111111)),
),
)
],
),
),
);
}
}
原因如下:
在之前的代码中,build
方法应该在State
类中实现,而不是在StatefulWidget
中实现。这是因为build
方法负责构建StatefulWidget
的UI,并且需要访问State
对象的状态和属性。
StatefulWidget
类本身只是一个容器,它持有与UI相关的状态,并且可以在State
类中进行处理和更新。因此,应该在State
类中实现build
方法。
在修改后的代码中,我将build
方法从StatefulWidget
类中移动到了State
类中的MessageItemState
类中。同时,还修复了MessageItemState
类中的createState
方法,返回了一个正确的MessageItemState
实例。