title: How to map array of object returning from api in Flutter?
body: <p>I have following REST api endpoint I need to consume from Flutter application. I have fromJson, toJson model mapping as usual.  How can I map these in to array.</p>
  <pre><code>[
    {
      &quot;id&quot;: 1,
      &quot;userId&quot;: 0,
      &quot;type&quot;: &quot;Create&quot;,
      &quot;tableName&quot;: &quot;Province&quot;,
      &quot;dateTime&quot;: &quot;2022-03-14T03:23:41.338932&quot;,
      &quot;oldValues&quot;: null,
      &quot;newValues&quot;: &quot;{\&quot;NameEN\&quot;:\&quot;Banteay Meanchey\&quot;,\&quot;NameKH\&quot;:\&quot;\\u1794\\u1793\\u17D2\\u1791\\u17B6\\u1799\\u1798\\u17B6\\u1793\\u1787\\u17D0\\u1799\&quot;,\&quot;Type\&quot;:\&quot;\\u1781\\u17C1\\u178F\\u17D2\\u178F\&quot;}&quot;,
      &quot;affectedColumns&quot;: null,
      &quot;primaryKey&quot;: &quot;{\&quot;Code\&quot;:1}&quot;
    },
    {
      &quot;id&quot;: 2,
      &quot;userId&quot;: 0,
      &quot;type&quot;: &quot;Create&quot;,
      &quot;tableName&quot;: &quot;Province&quot;,
      &quot;dateTime&quot;: &quot;2022-03-14T03:23:41.351456&quot;,
      &quot;oldValues&quot;: null,
      &quot;newValues&quot;: &quot;{\&quot;NameEN\&quot;:\&quot;Battambang\&quot;,\&quot;NameKH\&quot;:\&quot;\\u1794\\u17B6\\u178F\\u17CB\\u178A\\u17C6\\u1794\\u1784\&quot;,\&quot;Type\&quot;:\&quot;\\u1781\\u17C1\\u178F\\u17D2\\u178F\&quot;}&quot;,
      &quot;affectedColumns&quot;: null,
      &quot;primaryKey&quot;: &quot;{\&quot;Code\&quot;:2}&quot;
    }
  ]
  </code></pre>
  <p>I try to have following code but it doesnot work. One way is to return {} json object instead of [] array of object. But I do not want to change how API return the json.</p>
  <pre><code>Future&lt;List&lt;Audit&gt;&gt; getAudits() async {
      final jsonString = await dio.get('auditlogs');
      final List decodedJson = json.decode(jsonString.data.toString());
      final List&lt;Audit&gt; audits = decodedJson.map((e) =&gt; Audit.fromJson(e)).toList();
      return audits;
    }
  </code></pre>
  <p><strong>Update additional context</strong>
  I print the jsonString after</p>
  <pre><code>final jsonString = await dio.get('auditlogs');
  </code></pre>
  <p>Here below is the result. what wrong with this json.</p>
  <pre><code>[{id: 1, userId: 0, type: Create, tableName: Province, dateTime:
  2022-03-14T03:23:41.338932, oldValues: null, newValues:
  {&quot;NameEN&quot;:&quot;Banteay
  Meanchey&quot;,&quot;NameKH&quot;:&quot;\u1794\u1793\u17D2\u1791\u17B6\u1799\u1798\u17B6\u1
  793\u1787\u17D0\u1799&quot;,&quot;Type&quot;:&quot;\u1781\u17C1\u178F\u17D2\u178F&quot;},
  affectedColumns: null, primaryKey: {&quot;Code&quot;:1}}, {id: 2, userId: 0,
  type: Create, tableName: Province, dateTime:
  2022-03-14T03:23:41.351456, oldValues: null, newValues:
  {&quot;NameEN&quot;:&quot;Battambang&quot;,&quot;NameKH&quot;:&quot;\u1794\u17B6\u178F\u17CB\u178A\u17C6\u
  1794\u1784&quot;,&quot;Type&quot;:&quot;\u1781\u17C1\u178F\u17D2\u178F&quot;}, affectedColumns:
  null, primaryKey: {&quot;Code&quot;:2}}, {id: 3, userId: 0, type: Create,
  tableName: Province, dateTime: 2022-03-14T03:23:41.351487, oldValues:
  null, newValues: {&quot;NameEN&quot;:&quot;Kampong
  Cham&quot;,&quot;NameKH&quot;:&quot;\u1780\u17C6\u1796\u1784\u17CB\u1785\u17B6\u1798&quot;,&quot;Type
  &quot;:&quot;\u1781\u17C1\u178F\u17D2\u178F&quot;}, affectedColumns: null, primaryKey:
  {&quot;Code&quot;:3}}, {id: 4, userId: 0, type: Create, tableName: Province,
  dateTime: 2022-03-14T03:23:41.351503, oldValues: null, newValues:
  {&quot;NameEN&quot;:&quot;Kampong
  Chhnang&quot;,&quot;NameKH&quot;:&quot;\u1780\u17C6\u1796\u1784\u17CB\u1786\u17D2\u1793\u17
  B6\u17C6\u1784&quot;,&quot;Type&quot;:&quot;\u1781\u17C1\u178F\u17D2\u178F&quot;},
  affectedColumns: null, primaryKey: {&quot;Code&quot;:4}}, {id: 5, userId: 0,
  type: Create, tableName: Province, dateTime:
  2022-03-14T03:23:41.351515, oldValues: null, newValues:
  {&quot;NameEN&quot;:&quot;Kampong
  Speu&quot;,&quot;NameKH&quot;:&quot;\u178F\u1780\u17C6\u1796\u1784\u17CB\u179F\u17D2\u1796\
  u17BA&quot;,&quot;Type&quot;:&quot;\u1781\u17C1\u178F\u17D2\u178F&quot;}, affectedColumns: null,
  primaryKey: {&quot;Code&quot;:5}}, {id: 6, userId: 0, type: Create, tableName:
  Province, dateTime: 2022-03-14T03:23:41.351528, oldValues: null,
  newValues: {&quot;NameEN&quot;:&quot;Kampong
  Thom&quot;,&quot;NameKH&quot;:&quot;\u1780\u17C6\u1796\u1784\u17CB\u1792\u17C6&quot;,&quot;Type&quot;:&quot;\u1
  781\u17C1\u178F\u17D2\u178F&quot;}, affectedColumns: null, primaryKey:
  {&quot;Code&quot;:6}}]
  </code></pre>
  <p><strong>Data Model</strong></p>
  <pre><code>    import 'package:json_annotation/json_annotation.dart';
  
  part 'audit.g.dart';
  
  @JsonSerializable()
  class Audit {
    int id;
    int userId;
    String? type;
    String? tableName;
    DateTime dateTime;
    String? oldValues;
    String? newValues;
    String? affectedColumns;
    String? primaryKey;
  
    Audit(
      this.id,
      this.userId,
      this.type,
      this.tableName,
      this.dateTime,
      this.oldValues,
      this.newValues,
      this.affectedColumns,
      this.primaryKey,
    );
  
    factory Audit.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$AuditFromJson(json);
  
    Map&lt;String, dynamic&gt; toJson() =&gt; _$AuditToJson(this);
  }
  </code></pre>
  <p>Generated JSON Serialization</p>
  <pre><code>// GENERATED CODE - DO NOT MODIFY BY HAND
  
  // GENERATED CODE - DO NOT MODIFY BY HAND
  
  part of 'audit.dart';
  
  // **************************************************************************
  // JsonSerializableGenerator
  // **************************************************************************
  
  Audit _$AuditFromJson(Map&lt;String, dynamic&gt; json) =&gt; Audit(
        json['id'] as int,
        json['userId'] as int,
        json['type'] as String?,
        json['tableName'] as String?,
        DateTime.parse(json['dateTime'] as String),
        json['oldValues'] as String?,
        json['newValues'] as String?,
        json['affectedColumns'] as String?,
        json['primaryKey'] as String?,
      );
  
  Map&lt;String, dynamic&gt; _$AuditToJson(Audit instance) =&gt; &lt;String, dynamic&gt;{
        'id': instance.id,
        'userId': instance.userId,
        'type': instance.type,
        'tableName': instance.tableName,
        'dateTime': instance.dateTime.toIso8601String(),
        'oldValues': instance.oldValues,
        'newValues': instance.newValues,
        'affectedColumns': instance.affectedColumns,
        'primaryKey': instance.primaryKey,
      };
  </code></pre>