6

My JSON looks like this:

{
Info: [
       {
         c_type_id: "1",
         cleaning type: "A Cleaning"
        },
        {
          c_type_id: "2",
          cleaning type: "B Cleaning"
         },
         {
           c_type_id: "3",
           cleaning type: "C Cleaning"
         },
         {
           c_type_id: "4",
           cleaning type: "D Cleaning"
          },
          {
            c_type_id: "5",
            cleaning type: "E Cleaning"
           },
       ]
}

and here is the code: The following code is created by this Class 1:

class Album {
   List<Info> info;
   Album({this.info})
     Album.fromJson(Map<String, dynamic> json) {
      if (json['Info'] != null) {
       info =  List<Info>.empty();
       json['Info'].forEach((v) {
       info.add(new Info.fromJson(v));
      });
     }
    }

   Map<String, dynamic> toJson() {
     final Map<String, dynamic> data = new Map<String, dynamic>();
      if (this.info != null) {
      data['Info'] = this.info.map((v) => v.toJson()).toList();
      }
     return data;
     }
    }

class 2:

class Info {
  String cTypeId;
  String cleaningType;
  Info({this.cTypeId, this.cleaningType});
    Info.fromJson(Map<String, dynamic> json) {
    cTypeId = json['c_type_id'];
    cleaningType = json['cleaning type'];
    }
   Map<String, dynamic> toJson() {
   final Map<String, dynamic> data = new Map<String, dynamic>();
   data['c_type_id'] = this.cTypeId;
   data['cleaning type'] = this.cleaningType;
   return data;
 }
}

This is the error I get when I execute the code: error: The argument type 'List' can't be assigned to the parameter type 'String'.

enter image description here

Hoping for help!

8
  • check this flutter.dev/docs/cookbook/networking/fetch-data Commented Aug 9, 2021 at 6:32
  • If you get data From API refer my answer here or here Commented Aug 9, 2021 at 6:34
  • also the json data you provided is not a valid JSON. Commented Aug 9, 2021 at 6:35
  • @Ravindra S. Patil, I tried both but I didn't get output. Now, this error is showing "error: The operator '[]' isn't defined for the type 'Album'" Commented Aug 9, 2021 at 8:03
  • can you tell me your json data is host online? because my second answer json data and your json data is look like same Commented Aug 9, 2021 at 8:28

3 Answers 3

2

You should Try below code your problem has been solved ->

Declare your API Call funtion

Future<List<dynamic>> getInfoData() async {
    String url = 'https://fillmmaka.com/gigocleanapi/cleanintypes.php';
    var response = await http.get(Uri.parse(url), headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    });
    return json.decode(response.body)['Info'];
  }

Declare your Widget

Center(
    child: FutureBuilder<List<dynamic>>(
      future: getInfoData(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (context, index) {
                var id = snapshot.data[index]['c_type_id'];
                var type = snapshot.data[index]['cleaning type'];

                return Card(
                  shape: RoundedRectangleBorder(
                    side: BorderSide(
                      color: Colors.green.shade300,
                    ),
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: ListTile(
                    leading: Text(id),
                    title: Text(type),
                  ),
                );
              },
            ),
          );
        }
        return CircularProgressIndicator();
      },
    ),
  ),

Your Screen look like this -> enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

hi sir ravindra. can i comment here a follow-up question related to this problem? thanks
@KimLo Yes sure, say your problem
what if i have a similar list, but this time it is nested. How do i make each textfield of the sublist unique? bcoz in my program now, whenever i input 55 on the textfield, it will change all the textfield on the sublist with the same index. thanks! see image link. imgur.com/IjOqWPJ
0

What is your goal? What do you want to show on the screen? For example, do you want to show a list with all the cleaning types?

You are getting this error because the attribute info in your Album class is a List:

class Album {
   List<Info> info;

If you want to show the cleaningType of the first info on the list, you may do something like this:

return Text(snapshot.data.info[0].cleaningType);

Just keep in mind this will crash if the info list is empty.

1 Comment

and what if i want to show all the list data?
0

you are trying to access the whole list there just use snapshot.data.info[index].variableName you want to access and you will be good to go

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.