不知道问题出在哪里。请帮助我,因为我是新的Flutter
Widget代码:
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16),
child: ListView.builder(
itemBuilder: (context, index) {
print(sources);
print(articles);
return isSourceNews ?
SourceTile(
url: sources[index].url,
name: sources[index].name,
description: sources[index].description,
)
:
BlogTile(
imageUrl: articles[index].urlToImage,
title: articles[index].title,
description: articles[index].description,
url: articles[index].url,
) ;
},
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: articles.length,
),
),
)
字符串
用于从API获取数据的Dart文件代码
class SourceNews {
List<SourceModel> news = [];
Future<void> getSourceNews() async {
var url = Uri.parse(
"https://newsapi.org/v2/sources?language=$language&apiKey=$API_KEY");
var response = await http.get(url);
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == 'ok') {
for(var element in jsonData['sources']){
if (element['url'] != null && element['description'] != null) {
SourceModel sourceModel = SourceModel(
name: element['name'],
url: element['url'],
description: element['description'],
);
if(sourceModel!=null) {
news.add(sourceModel);
}
}
}
//
// jsonData['sources'].forEach((element) {
// if (element['url'] != null && element['description'] != null) {
// SourceModel sourceModel = SourceModel(
// name: element['name'],
// description: element['description'],
// url: element['url'],
// );
// if(sourceModel!=null) {
// news.add(sourceModel);
// }
//
// }
// });
}
}
}
型
错误堆栈:
======== Exception caught by widgets library =======================================================
The following RangeError was thrown building:
RangeError (index): Invalid value: Not in inclusive range 0..2: 3
When the exception was thrown, this was the stack:
#0 List.[] (dart:core-patch/growable_array.dart:264:36)
#1 _HomeState.dashboard.<anonymous closure> (package:news_app/views/home.dart:398:41)
#2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/scroll_delegate.dart:489:22)
#3 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:828:28)
#4 SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:842:55)
#5 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2720:19)
#6 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:834:12)
#7 RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:351:23)
#8 RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:2506:59)
#9 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:1062:15)
#10 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:2506:14)
#11 RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:340:5)
#12 RenderSliverMultiBoxAdaptor.insertAndLayoutChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:488:5)
#13 RenderSliverList.performLayout.advance (package:flutter/src/rendering/sliver_list.dart:241:19)
#14 RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:283:12)
#15 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#16 RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:139:12)
#17 RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:361:11)
#18 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#19 RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:534:13)
#20 RenderShrinkWrappingViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1882:12)
#21 RenderShrinkWrappingViewport.performLayout (package:flutter/src/rendering/viewport.dart:1833:20)
#22 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#23 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#24 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#25 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#26 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#27 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#28 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#29 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#30 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#31 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#32 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#33 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#34 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#35 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#36 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#37 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#38 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#39 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#40 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#41 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#42 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#43 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#44 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#45 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#46 _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1449:11)
#47 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#48 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#49 RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:238:12)
#50 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#51 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#52 ChildLayoutHelper.layoutChild (package:flutter/src/rendering/layout_helper.dart:52:11)
#53 RenderFlex._computeSizes (package:flutter/src/rendering/flex.dart:868:45)
#54 RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:903:32)
#55 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#56 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#57 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:122:14)
#58 RenderObject.layout (package:flutter/src/rendering/object.dart:2395:7)
#59 RenderBox.layout (package:flutter/src/rendering/box.dart:2386:11)
#60 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:171:12)
#61 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:1056:7)
#62 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:235:7)
#63 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:403:14)
#64 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:2234:7)
#65 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1016:18)
#66 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:492:19)
#67 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:905:13)
#68 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:358:5)
#69 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1284:15)
#70 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1214:9)
#71 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1072:5)
#72 _invoke (dart:ui/hooks.dart:142:13)
#73 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:359:5)
#74 _drawFrame (dart:ui/hooks.dart:112:31)
====================================================================================================
型
尝试检查循环不会在空数据上运行或越界,但没有找到解决方案,API数据完全正常,我认为错误是在溢出语法中。
1条答案
按热度按时间8i9zcol21#
有两个对象列表来显示数据,但您仍然对源列表使用相同的itemCount。
字符串