我在flutter中使用网格视图时遇到了这个问题。控制台给了我以下错误消息,但是代码可以正常工作。 Flutter GridView错误:构建PictureLinkGrid时引发了以下RangeError(脏,相关性:
这是我的代码。
class _PictureLinkGridState extends State<PictureLinkGrid> {
@override
Widget build(BuildContext context) {
final pictureData = Provider.of<List<ImageProperty>>(context) ?? [];
final neededPicture = [];
final demoPicture = [];
//final user = Provider.of<SystemUser>(context);
int count = 0;
// get Demo Picture
pictureData.forEach((picture) {
if (picture.title.contains('demo')) {
demoPicture.add(picture);
}
});
// get Needed Picture
pictureData.forEach((picture) {
if (picture.display_count < 10 && !picture.title.contains('demo')
//&& picture.creator_uid != user.uid
) {
//print('${picture.creator_uid} != ${user.uid}');
print('${picture.title} is NOT null');
setState(() {
picture.isShown = true;
});
neededPicture.add(picture);
} else {
print('${picture.title} is null');
}
});
// fill in the empty picture
count = 0;
while (neededPicture.length < 9) {
neededPicture.add(demoPicture[count]);
count++;
}
return GridView.builder(
itemCount: neededPicture.length,
shrinkWrap: true,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
print(
'Picture title in picturelink grid: ${neededPicture[index].title}');
return TouchableWebImageCard(
imagePath: neededPicture[index].url,
imageTitle: neededPicture[index].title,
);
});
}
}
这是错误消息
═╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following RangeError was thrown building PictureLinkGrid(dirty, dependencies:
[_InheritedProviderScope<List<ImageProperty>>], state: _PictureLinkGridState#58490):
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was:
PictureLinkGrid
lib/…/camera/image_evaluation.dart:211
When the exception was thrown, this was the stack:
#0 List.[] (dart:core-patch/growable_array.dart:166:60)
#1 _PictureLinkGridState.build
package:PhotoEarn/…/camera/picturelink_grid.dart:49
#2 StatefulElement.build
package:flutter/…/widgets/framework.dart:4663
#3 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4546
#4 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4719
#5 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#6 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4525
#7 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4710
#8 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
#9 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#10 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5991
... Normal element mounting (118 frames)
#128 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#129 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5991
... Normal element mounting (79 frames)
#208 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#209 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
#210 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#211 _InheritedProviderScopeElement.performRebuild
package:provider/src/inherited_provider.dart:397
#212 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#213 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4525
#214 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
... Normal element mounting (7 frames)
#221 SingleChildWidgetElementMixin.mount
package:nested/nested.dart:223
... Normal element mounting (140 frames)
#361 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#362 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
#363 RenderObjectElement.updateChildren
package:flutter/…/widgets/framework.dart:5624
#364 MultiChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:6001
#365 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#366 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#367 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4719
#368 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#369 StatefulElement.update
package:flutter/…/widgets/framework.dart:4751
#370 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#371 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#372 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#373 ProxyElement.update
package:flutter/…/widgets/framework.dart:4906
#374 _InheritedNotifierElement.update
package:flutter/…/widgets/inherited_notifier.dart:183
#375 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#376 SingleChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:5881
#377 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#378 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#379 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4719
#380 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#381 StatefulElement.update
package:flutter/…/widgets/framework.dart:4751
#382 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#383 SingleChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:5881
#384 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#385 SingleChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:5881
#386 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#387 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#388 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#389 StatelessElement.update
package:flutter/…/widgets/framework.dart:4627
#390 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#391 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#392 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#393 ProxyElement.update
package:flutter/…/widgets/framework.dart:4906
#394 Element.updateChild
package:flutter/…/widgets/framework.dart:3245
#395 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
#396 StatefulElement.performRebuild
#397 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#398 BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2667
#399 WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:866
#400 RendererBinding._handlePersistentFrameCallback
package:flutter/…/rendering/binding.dart:286
#401 SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1117
#402 SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1056
#403 SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:972
#407 _invoke (dart:ui/hooks.dart:253:10)
#408 _drawFrame (dart:ui/hooks.dart:211:3)
(elided 3 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building PictureLinkGrid(dirty, dependencies: [_InheritedProviderScope<List<ImageProperty>>], state: _PictureLinkGridState#58490):
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was
PictureLinkGrid
lib/…/camera/image_evaluation.dart:211
When the exception was thrown, this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:166:60)
#1 _PictureLinkGridState.build
package:PhotoEarn/…/camera/picturelink_grid.dart:49
#2 StatefulElement.build
package:flutter/…/widgets/framework.dart:4663
#3 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4546
#4 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4719
...
════════════════════════════════════════════════════════════════════════════════
尽管我收到此错误消息,但我的应用程序似乎可以正常运行。有什么问题?我该如何解决? 我期待着大家的回音。谢谢。