parveen
parveen

Reputation: 99

How to make api call of string in flutter?

I am learning Flutter. This is the string that I need to call and I don't know how to call this type of string.

{
   "Info":[
      {
         "c_type_id":"1",
         "cleaning type":"Washroom Cleaning"
      },
      {
         "c_type_id":"2",
         "cleaning type":"Garden\/Lawn Cleaning"
      }
   ]
}

My code

  class Album {
    final String title;
     Album({
      this.title,
     });
     factory Album.fromJson(Map<String, dynamic> json) {
       return Album(
       title: json['title'],
      );
     }
    }

As I am following my code like this https://flutter.dev/docs/cookbook/networking/fetch-data and got this error "A non-null String must be provided to a Text widget." because they are following this type of string and my string type is different. Help!

{
 "userId": 1,
  "id": 1,
  "title": "quidem molestiae enim"
 }

Upvotes: 1

Views: 610

Answers (2)

Muhtar
Muhtar

Reputation: 1702

Your question is not clear at all but I can show you an example how to reach an element in a json. Your json consists a list that contains string keys. To reach first element of list;

json["Info"][0]

Then pass the key you want to get its value;

json["Info"][0]["c_type_id"]

http_requests All type of http requests are mentioned in this post. If you have any further problem kindly make comment.

Upvotes: 1

Abbasihsn
Abbasihsn

Reputation: 2171

Your given data (API response) seems to have a list of maps, so you should get the data first (use async function):

var response = await http.get("Your Url")

then you should extract the given map list as follow:

var temp = json.decode(response);
List<dynamic> result = temp['Info']); // it is the described list

now, you can use your data (extract each parameter):

String c_type_id = result[0]['c_type_id'];
// and so on...

Upvotes: 1

Related Questions