2

I'm just new in flutter and I think this is a newbie question.

I'm trying to get the data that I call on my API and save it to my model but it's having an error type 'List' is not a subtype of type 'Map<dynamic, dynamic>'

Here is the copy of my model

class AdTemplate{
  final int id; 
  final String filePath; 
  final String notification; 
  final String status; 
  final int whenAdded; 

  AdTemplate(
      {this.id, 
      this.filePath, 
      this.notification,
      this.status, 
      this.whenAdded});

  factory AdTemplate.fromJson(Map<String, dynamic> json) {
    return  AdTemplate(
      id: json['ID'], 
      filePath: json['FilePath'],
      notification: json['Notification'], 
      status: json['Status'], 
      whenAdded: json['WhenAdded']
    );
  }
}

And this is my function

Future<AdTemplate> getActiveBannerNotif() async {  
    try { 
      String url = 'https://api/path/'; 
      var res = await http.get(url);  
      final Map data = convert.jsonDecode(res.body);

      if (res.statusCode == 200) { 
        print("Data Fetch!");    
        AdTemplate template = AdTemplate.fromJson(data);  

        return template;
      } else {
        print('No data.'); 

        return null;
      }
    } catch (e) {
      print(e);
      return null;
    } 
}

This is the sample data that I get from the API

[{"ID":49,"FilePath":"20210903t171244.png","Notification":"ACT","WhenAdded":1630689165,"Status":"INA"}]
6
  • in wich line is the error? Commented Oct 13, 2021 at 15:32
  • @MiguelEscobarCalderon this line AdTemplate template = AdTemplate.fromJson(data); Commented Oct 13, 2021 at 15:36
  • can you add your JSON string to the question Commented Oct 13, 2021 at 15:39
  • @rosh-dev I added it. Commented Oct 13, 2021 at 15:43
  • your receiving an array. that causes the error. change your API or code Commented Oct 13, 2021 at 15:43

3 Answers 3

1

API returns JSON array not json object so that is List not Map.

try :

if (res.statusCode == 200) { 
    print("Data Fetch!");    
    AdTemplate template = AdTemplate.fromJson(json.decode(utf8.decode(res.bodyBytes)));  

    return template;
  }
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your suggestion but its the same error type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
show me all the new code (getActiveBannerNotif)
0

You are receiving an array from your API and that causes the error. change as following to access a single element from array

Future<AdTemplate> getActiveBannerNotif() async {  
    try { 
      String url = 'https://api/path/'; 
      var res = await http.get(url);  
      
      if (res.statusCode == 200) { 
        print("Data Fetch!");  
        final data = convert.jsonDecode(res.body);
        AdTemplate template = AdTemplate.fromJson(data[0]);  

        return template;
      } else {
        print('No data.'); 

        return null;
      }
    } catch (e) {
      print(e);
      return null;
    } 
}

Comments

0

Your api return a List

[{"ID":49,"FilePath":"20210903t171244.png","Notification":"ACT","WhenAdded":1630689165,"Status":"INA"}]

Try to get data like that: data[0] or data.first before convert into model

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.