listview - Flutter listView builder不断给出此错误:“RangeError(索引):无效值:不在0..19范围内,包括:20”
问题描述
我有这个新闻 ApI,它会获取最新的新闻更新,但即使我有一个项目“itemCount”到它的长度,我的列表视图也会给我一个错误。
我在 Itemcount 上尝试了“_total.length”,但它给了我一个错误.. 它说“没有为类 'int' 定义 getter 'length'。
尝试导入定义 'length' 的库,将名称更正为现有 getter 的名称,或定义名为 'length' 的 getter 或字段。
class _NewsUpdateState extends State<NewsUpdate> {
List _bottomItems =
[
{"icon": FontAwesomeIcons.fire, "index": 0},
{"icon": FontAwesomeIcons.moneyBillAlt, "index": 1},
{"icon": FontAwesomeIcons.bell, "index": 2},
{"icon": FontAwesomeIcons.futbol, "index": 3},
];
String _apiKey = '46d80623786da4a97847da2b6cd7747';
int _currentTab = 0;
String _apiUrl = '';
int _total = 0;
List _articles = [];
bool _loading = true;
@override
void initState() {
super.initState();
changeTab(index: 0);
}
void changeTab({int index = 0}) {
//tab1 is about top-headlines
//tab2 is about bitcoins
//tab3 = apple
//tab4 = techcrunch
switch (index) {
case 0:
_apiUrl =
'https://newsapi.org/v2/top-headlines?country=za&apiKey=' +
_apiKey;
break;
case 1:
_apiUrl =
'https://newsapi.org/v2/top-headlines?country=za&category=business&apiKey=' +
_apiKey;
break;
case 2:
_apiUrl =
'https://newsapi.org/v2/top-headlines?country=za&category=entertainment&apiKey=' +
_apiKey;
break;
case 3:
_apiUrl =
'https://newsapi.org/v2/top-headlines?country=za&category=sports&apiKey=' +
_apiKey;
break;
}
print(_apiUrl);
setState(() {
_loading = true;
_total = 0;
_articles = [];
});
http.get(_apiUrl).then((response) {
var data = json.decode(response.body);
setState(() {
_total = data['totalResults'];
_articles = data['articles'];
_loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Latest News",
style: TextStyle(
color: Colors.black, fontSize: 25.0, fontWeight: FontWeight.bold),
),
elevation: 5.0,
backgroundColor: Colors.white,
centerTitle: true,
),
bottomNavigationBar: _buildBottomNavigation(context),
body: new SafeArea(child: _buildBody(context)),
);
}
_buildBottomNavigation(BuildContext context) {
var _items = <BottomNavigationBarItem>[];
for (var item in _bottomItems) {
_items.add(new BottomNavigationBarItem(
icon: new Icon(
item['icon'],
color: Colors.black,
),
title: new Text(''),
));
}
return new BottomAppBar(
color: Colors.white,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: _bottomItems.map((x) {
return new IconButton(
icon: new Icon(
x['icon'],
color: _currentTab == x['index'] ? Colors.black : Colors.black38,
),
onPressed: () {
setState(() {
_currentTab = x['index'];
});
changeTab(index: x['index']);
},
);
}).toList(),
),
);
}
_buildBody(BuildContext context) {
if (_loading) {
return new SpinKitCircle(
color: Colors.black,
size: 50.0,
);
}
print(_articles);
return new ListView.builder(
itemBuilder: (context, int index) {
return new Padding(
padding: new EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: new ListItem(data: _articles[index]),
);
},
itemCount: _total,
);
}
}
这应该将 api 的总结果的实际长度设置为 itemcount
解决方案
我遇到过同样的问题:
您还必须传递itemCount
参数
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return Text(list[index].description);
}),
推荐阅读
- javascript - 查找可见项目的索引并选择要在新窗口中打开的 url
- mysql - 通过 OR 优化左连接与多个连接?
- java - ISIS:附件收集问题(Blob、Clob)
- apache-camel - Apache Camel:根据值切换路由
- xml - 什么决定了摆放位置
签署 SAML 文档时标记? - sql - 分离和附加分区 db2 版本 10.5 给出 sqlstate 55007 错误
- java - 在 JFrame 上自动生成新的 JPanel
- r - 将数据格式从 DataTime 更改为通用 r
- javascript - 如何从 UserFrosting 控制器正确返回 .xls Excel 文件并在 Angular 2+ 中处理它?
- javascript - 如何使用相同的按钮打开和关闭我的汉堡菜单?