LateInitializationError on Flutter 3.7.0 (solution included)
After upgrading to Flutter 3.7.0 an error popped on a specific scenario using the StaggeredGridView:
@override
Widget build(BuildContext context) {
return StaggeredGrid.count(
crossAxisCount: 7,
mainAxisSpacing: 1,
crossAxisSpacing: 1,
children: [
StaggeredGridTile.count(
crossAxisCellCount: 7,
mainAxisCellCount: 1,
child: FittedBox(
fit: BoxFit.contain,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(DateFormat(DateFormat.MONTH).format(DateTime.now())),
),
),
)
],
);
}
Throws:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following LateError was thrown building PixelsCalendar:
LateInitializationError: Field '_children@38042623' has not been initialized.
The relevant error-causing widget was:
PixelsCalendar
PixelsCalendar:file:///Users/teovogel/Documents/GitHub/pixels_flutter/lib/core/main/pixels/pixels_screen.dart:34:19
When the exception was thrown, this was the stack:
#0 MultiChildRenderObjectElement._children (package:flutter/src/widgets/framework.dart)
#1 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6473:33)
#2 Element.debugDescribeChildren (package:flutter/src/widgets/framework.dart:4576:5)
#3 DiagnosticableTreeNode.getChildren (package:flutter/src/foundation/diagnostics.dart:3003:48)
#4 TextTreeRenderer._debugRender (package:flutter/src/foundation/diagnostics.dart:1219:43)
#5 TextTreeRenderer.render (package:flutter/src/foundation/diagnostics.dart:1151:14)
#6 DiagnosticsNode.toStringDeep.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:1816:9)
#7 DiagnosticsNode.toStringDeep (package:flutter/src/foundation/diagnostics.dart:1823:6)
#8 DiagnosticsNode.toString.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:1734:18)
#9 DiagnosticsNode.toString (package:flutter/src/foundation/diagnostics.dart:1746:6)
#10 Diagnosticable.toString.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:3131:78)
#11 Diagnosticable.toString (package:flutter/src/foundation/diagnostics.dart:3133:6)
#12 _StringBase._interpolate (dart:core-patch/string_patch.dart:851:19)
#13 Scrollable.of.<anonymous closure> (package:flutter/src/widgets/scrollable.dart:336:27)
#14 Scrollable.of (package:flutter/src/widgets/scrollable.dart:348:6)
#15 StaggeredGrid.createRenderObject (package:flutter_staggered_grid_view/src/widgets/staggered_grid.dart:85:22)
#16 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5817:52)
#17 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6519:11)
... Normal element mounting (13 frames)
#30 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#31 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6512:36)
#32 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6524:32)
... Normal element mounting (25 frames)
#57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#58 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6512:36)
#59 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6524:32)
... Normal element mounting (111 frames)
#170 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (49 frames)
#219 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#220 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6512:36)
#221 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6524:32)
... Normal element mounting (142 frames)
#363 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#364 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6512:36)
#365 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6524:32)
... Normal element mounting (172 frames)
#537 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#538 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6512:36)
#539 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6524:32)
... Normal element mounting (492 frames)
#1031 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1038 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#1045 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1052 _NestedHookElement.mount (package:nested/nested.dart:187:11)
... Normal element mounting (7 frames)
#1059 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#1066 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1073 _NestedHookElement.mount (package:nested/nested.dart:187:11)
... Normal element mounting (7 frames)
#1080 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#1087 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1094 _NestedHookElement.mount (package:nested/nested.dart:187:11)
... Normal element mounting (7 frames)
#1101 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#1108 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1115 _NestedHookElement.mount (package:nested/nested.dart:187:11)
... Normal element mounting (7 frames)
#1122 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#1129 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1136 _NestedHookElement.mount (package:nested/nested.dart:187:11)
... Normal element mounting (7 frames)
#1143 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#1150 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1157 _NestedHookElement.mount (package:nested/nested.dart:187:11)
... Normal element mounting (7 frames)
#1164 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#1171 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#1172 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18)
#1173 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1176:16)
#1174 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5)
#1175 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1092:18)
#1176 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2682:19)
#1177 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1091:13)
#1178 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:926:7)
#1179 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:906:7)
#1183 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)
(elided 3 frames from class _Timer and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════
SOLUTION
Adding the parameter "axisDirection" solves the issue. Maybe it should be a required parameter from now on?
@override
Widget build(BuildContext context) {
return StaggeredGrid.count(
axisDirection: AxisDirection.down, // <----- Add this line
crossAxisCount: 7,
mainAxisSpacing: 1,
crossAxisSpacing: 1,
children: [
StaggeredGridTile.count(
crossAxisCellCount: 7,
mainAxisCellCount: 1,
child: FittedBox(
fit: BoxFit.contain,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(DateFormat(DateFormat.MONTH).format(DateTime.now())),
),
),
)
],
);
}
So much time saved, thanks for taking the time to post the solution. Just for future reference, how did you figure that solution out? I was completely lost...
@tiger5226 Glad it helped! I discovered it by pure chance, I was using the grid view with that parameter in other part of my app, where it wasn't crashing
Great solution! Thank you!
nice info brow
you save my day
Very nice brow 🤙
Encountered the same error. Would be nice, if the parameter would be required for newer flutter versions. :)
I might love you
Love you!
I don't reproduce with Flutter 3.7.10, do you?
Thankyou for this solution
nice