1

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"
 }
quoci
  • 2,940
  • 1
  • 9
  • 21
parveen
  • 69
  • 7

2 Answers2

1

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...
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abbasihsn
  • 2,075
  • 1
  • 7
  • 17
  • Sorry for the noob question but where to add var temp = json.decode(response); List result = temp['Info']); – parveen Jun 10 '21 at 08:01
  • you can use these commands in the "then" function! as you maybe know, http.get is an async function and after getting the result, you can call .then function! – Abbasihsn Jun 10 '21 at 11:58
1

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.

Muhtar
  • 1,506
  • 1
  • 8
  • 33
  • i used your method like this class Album { final String title; final String id; Album({ this.title, this.id, }); factory Album.fromJson(Map json) { return Album( title: json['Info'][0], id: json["Info"][0]["c_type_id"], ); } } and this is showing this message on screen "type 'internalLinkedHashMap' is not subtype of 'String'" :( where I am wrong – parveen Jun 10 '21 at 07:46
  • you can not use { title: json['Info'][0] } like this. When you get this property it inclueds a list includes a map. json['Info'][0] = [{"c_type_id":"1","cleaning type":"WashroomCleaning"}, {"c_type_id":"2","cleaning type":"Garden\/Lawn Cleaning"}]. This is not a string – Muhtar Jun 10 '21 at 08:00