简体   繁体   English

列表视图中一次仅显示一项

[英]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.

相关问题 视图中的脚本仅影响列表中的一项 - Script in view only affect one item in a list 应用发明者仅一次从列表中选择随机项目 - app inventor selecting random item from a list one time only 列表仅显示一项 - list displays only one item 我的迭代只会返回我列表中的最后一项,而不是一次返回每个项目以传递给另一个函数以在主列表中使用 - My iteration will only return the last item in my list, not return each item one at a time to pass to another function for use in a master list 通过 Python 一次列出一项 - Move Thru a Python List One Item at a Time (Android)在对话框中列出,只有一个带复选框的项目 - (Android) List in dialog with only one item with a checkbox 只搜索列表的每个元素一次 - seraching every element of a list for only one time 将每个项目的所有数据加载到列表视图中还是仅加载所需的信息? - Load all data of each item in list view or only needed information? 给定未知长度列表,通过仅扫描一次来返回其中的随机项 - Given an unknown length list, return a random item in it by scanning it only 1 time 如何显示列表中一项的视图并隐藏其他项? - How to show view for one item in a list and hide from others.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM