0

I have a list of users that I am reading from JSON.

This is the JSON file:

{
"Dependents": [
    {
      "Name": "Kim",
      "Relationship": "Parent"
    },
    {
      "Name": "Tim",
      "Relationship": "Spouse"
    }
  ]
}

This is the model class:

new_fifth_model.dart

class NewFifthModel {

  String name;
  String relationship;

  NewFifthModel(this.name, this.relationship);
}

And this is the class to bring out the users in a list.

NewFifth.dart

import 'package:flutter/material.dart';
import 'package:emas_app/model/new_fifth_model.dart';
import 'dart:convert';
import 'dart:async' show Future;
import 'package:http/http.dart' as http;

final String url = "http://crm.emastpa.com.my/MemberInfo.json";
final int page = 5;

//Future to get list of dependent names
Future<List<NewFifthModel>> fetchUserInfo() async{

  var response = await http.get(url, headers: {"Accept": "application/json"});

  List data = json.decode(response.body)["Dependents"];
  var fifthmodel = <NewFifthModel>[];
  data.forEach((f) => fifthmodel.add(new NewFifthModel(f["Name"], f["Relationship"])));

  print(fifthmodel);

  return fifthmodel;
}

class NewFifth extends StatefulWidget {
  @override
  _FifthState createState() => _FifthState();
}

class _FifthState extends State<NewFifth> {

  List<NewFifthModel> fifthList;

  @override
  void initState() {
    super.initState();
    if (fifthList == null) {
      fetchUserInfo().then((data) {
        this.setState(() {
          fifthList = data;
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {

    //body widget
    Widget _createBody() {

      if(fifthList == null){
        return new Center(
          child: new CircularProgressIndicator(),
        );
      }

      else{
        return new ListView.builder(
            shrinkWrap: true,
            itemCount: fifthList.length,
            itemBuilder: (context, index){
              return new Column(
                children: fifthList.map((f){
                   return new Card(
                    child: new ListTile(
                      title: new Text(f.name),
                      subtitle: new Text(f.relationship),
                      trailing: new Text(index.toString()),
                      onTap: (){
                        makeDialog(index.toString());
                      },
                    ),
                  );
                }).toList(),
              );
            });
      }
    }

    return new Scaffold(
      body: _createBody(),
    );
  }
}

This is the output on the screen.

Duplicate Index number

The problem I am having (as you can see in the picture) is that the index number I put in the trailing part of the ListTile is duplicating and I really need the index number in order to proceed.

How do I rectify this problem?

Any help is very much appreciated.

2 Answers 2

1

you are creating 2 list here, you are recreating a Column with the entire list inside the item build, the ListView.builder is already taking care of iterating on your list using the itemCount.

itemBuilder: (context, index) {
          final f = fifthList[index];
          return Card(
                child: new ListTile(
                  title: new Text(f.name),
                  subtitle: new Text(f.relationship),
                  trailing: new Text(index.toString()),
                  onTap: (){
                    makeDialog(index.toString());
                  },
                ),
              );
}
Sign up to request clarification or add additional context in comments.

1 Comment

A simple answer and a correct one. Thanks a lot, Hadrien.
1

Looks like you have only 2 items in the JSON object but you are showing 4.

I think you meant to only show 2? If so, in your itemBuilder function, you should do this:

return new ListView.builder(
    shrinkWrap: true,
    itemCount: fifthList.length,
    itemBuilder: (context, index) {
      var f = fifthList[index];
      return new Card(
        child: new ListTile(
          title: new Text(f.name),
          subtitle: new Text(f.relationship),
          trailing: new Text(index.toString()),
          onTap: () {
            makeDialog(index.toString());
          },
        ),
      );
    });

You were using .map() which looped through the list again on each item. You had 2 items, so you ended up with 4. If you had 3, it would show 6 items, and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.