[英]Only one item is appearing at a time on list view
I cant seem to figure out how to get all of my items in my list to display in the list view 我似乎无法弄清楚如何将列表中的所有商品显示在列表视图中
Currently, when I click my button to display the list, only one items shows up. 当前,当我单击我的按钮以显示列表时,仅显示一个项目。 If I click back, and click main button again, it shows 2 items from the list.
如果我单击返回,然后再次单击主按钮,它将显示列表中的2个项目。 Rinse and repeat, 3 items.
冲洗并重复3个项目。 I can't seem to debug with print statements to see where my error lies.
我似乎无法使用打印语句进行调试,以查看错误所在。 When I try print(trails) or other variations, it says Instance of trail model (not very helpful).
当我尝试打印(轨迹)或其他变体时,它说轨迹模型的实例(不是很有帮助)。 Any ideas?
有任何想法吗?
Here's my code: 这是我的代码:
class HomeScreen extends State<MyApp> {
int counter = 0;
Future<List<TrailModel>> fetchData() async {
counter++;
var response = await get(
'https://www.hikingproject.com/data/get-trails?lat=39.733694&lon=-121.854771&maxDistance=10&key=200419778-6a46042e219d019001dd83b13d58aa59');
final trailModel = TrailModel.fromJson(json.decode(response.body));
//trails.add(trailModel);
setState(() {
trails.add(trailModel);
});
return trails;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("HikeLocator")),
body: new RaisedButton(
child: Text("click me"),
onPressed: () async {
final trails = await fetchData();
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new ListScreen(trails)),
);
},
),
));
}
}
class ListScreen extends StatelessWidget {
final List<TrailModel> trails;
ListScreen(this.trails);
@override
Widget build(BuildContext ctxt) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Here are your trails"),
),
body: TrailList(trails),
);
}
}
class TrailList extends StatelessWidget {
final List<TrailModel> trails;
TrailList(this.trails);
Widget build(context) {
return ListView.builder(
itemCount: trails.length,
itemBuilder: (context, int index) {
Object myText = json.encode(trails[index].trails);
List<dynamic> myText2 = json.decode(myText);
return Text(myText2[index]['name']);
},
);
}
}
class TrailModel {
Object trails;
TrailModel(this.trails);
TrailModel.fromJson(Map<String, dynamic> parsedJson) {
trails = parsedJson['trails'];
}
}
I think my problem might lie in the fetchData(), but I'm not entirely sure. 我认为我的问题可能出在fetchData()上,但我不确定。 Trying to at least print out the values to limit where my problem might be.
尝试至少打印出值以限制我的问题所在。 (Is it only adding 1 to the list each time I press the button? Is it only rendering one when I click it? Is it fetching all the data each click or only one json object? etc.)
(是否是每次按下按钮时都只将1加到列表中?单击时是否仅渲染一个?每次单击时获取所有数据还是仅获取一个json对象?等)
Thank you kindly for any assistance. 谢谢您的协助。 Sorry, I'm kind of new to dart, so this is a huge learning curve for
抱歉,我是飞镖的新手,所以这对于
There are a couple problems in your code. 您的代码中有几个问题。 The main reason this doesn't work as you expect is because you're parsing all elements of the json into one
TrailModel
object, but then your code assumes that you'll have multiple TrailModel
objects. 不能按预期工作的主要原因是因为您将json的所有元素解析为一个
TrailModel
对象,但是随后您的代码假定您将拥有多个TrailModel
对象。
The easiest way to fix it up and get it working is to use the list from TrailModel.trails
instead of the one in the widget. 修复并使其正常运行的最简单方法是使用
TrailModel.trails
的列表,而不是小部件中的列表。
First, in ListScreen
, pass just the first element in the list. 首先,在
ListScreen
,仅传递列表中的第一个元素。
class ListScreen extends StatelessWidget {
final List<TrailModel> trails;
...
@override
Widget build(BuildContext ctxt) {
return new Scaffold(
...
body: TrailList(trails.first),
);
}
}
Then, in TrailList
, use the trails
list you have from TrailModel
: 然后,在
TrailList
,使用从TrailModel
获得的trails
列表:
class TrailList extends StatelessWidget {
final TrailModel model;
TrailList(this.model);
Widget build(context) {
return ListView.builder(
itemCount: model.trails.length,
itemBuilder: (context, int index) {
final trail = model.trails[index];
...
},
);
}
}
When I try print(trails) or other variations, it says Instance of trail model (not very helpful)
当我尝试打印(轨迹)或其他变体时,它说轨迹模型的实例(不是很有帮助)
print
uses the output of the toString
method in your classes. print
在类中使用toString
方法的输出。 You're seeing Instance of trail model
because that's the default implementation you get from the super class Object
. 您将看到
Instance of trail model
因为这是您从超类Object
获得的默认实现。 You can override it to get something more useful: 您可以覆盖它以获得更多有用的信息:
class TrailModel {
@override
String toString() => 'trails=$trails';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.