title: FLUTTER : Convert a Json object to model
body: <p>I have been trying to solve this issue from 2 days but couldn't get through it.</p>
  <pre class="lang-json prettyprint-override"><code>[
      {
          &quot;_id&quot;: &quot;60bcd42bd8fb4d2dabca24af&quot;,
          &quot;title&quot;: &quot;test article&quot;,
          &quot;article&quot;: &quot;{\&quot;insert\&quot;:\&quot;This is some test file\\n\\ncode block 1\&quot;},{\&quot;insert\&quot;:\&quot;\\n\&quot;,\&quot;attributes\&quot;:{\&quot;code-block\&quot;:true}},{\&quot;insert\&quot;:\&quot;quote block 1\&quot;},{\&quot;insert\&quot;:\&quot;\\n\&quot;,\&quot;attributes\&quot;:{\&quot;blockquote\&quot;:true}}\&quot;&quot;,
          &quot;author&quot;: &quot;Ashutosh&quot;,
          &quot;createdAt&quot;: &quot;2021-06-06T13:56:59.346Z&quot;,
          &quot;updatedAt&quot;: &quot;2021-06-06T13:56:59.346Z&quot;,
          &quot;__v&quot;: 0
      }
  ]
  </code></pre>
  <p>This is the response i will get from the server and I will use the <code>article</code> field for my rich text editor in flutter <code>flutter_quill</code> package.</p>
  <pre class="lang-dart prettyprint-override"><code>import 'package:dio/dio.dart';
  import 'package:learnlinux/model/articles.dart';
  
  class Network {
    static final String baseUrl = &quot;http://localhost:3000&quot;;
    static final String articles = &quot;/articles&quot;;
  
    Future&lt;List&lt;Article&gt;&gt; getArticle() async {
      try {
        final Response response = await Dio().get(baseUrl + articles);
        print(response.statusCode);
        if (response.statusCode == 200) {
          print(response.data);
          return articleFromJson(response.data);
        } else {
          throw Exception(&quot;Okay we have an error&quot;);
        }
      } on DioError catch (e) {
        print(e.response?.data);
        throw Exception(e.response?.statusCode);
      }
    }
  }
  </code></pre>
  <p>Model Class</p>
  <pre><code>import 'dart:convert';
  
  List&lt;Article&gt; articleFromJson(String str) =&gt;
      List&lt;Article&gt;.from(json.decode(str).map((x) =&gt; Article.fromJson(x)));
  
  String articleToJson(List&lt;Article&gt; data) =&gt;
      json.encode(List&lt;dynamic&gt;.from(data.map((x) =&gt; x.toJson())));
  
  class Article {
    Article({
      required this.id,
      required this.title,
      required this.article,
      required this.author,
      required this.createdAt,
      required this.updatedAt,
    });
  
    String id;
    String title;
    String article;
    String author;
    DateTime createdAt;
    DateTime updatedAt;
  
    factory Article.fromJson(Map&lt;String, dynamic&gt; json) =&gt; Article(
          id: json[&quot;_id&quot;],
          title: json[&quot;title&quot;],
          article: json[&quot;article&quot;],
          author: json[&quot;author&quot;],
          createdAt: DateTime.parse(json[&quot;createdAt&quot;]),
          updatedAt: DateTime.parse(json[&quot;updatedAt&quot;]),
        );
  
    Map&lt;String, dynamic&gt; toJson() =&gt; {
          &quot;_id&quot;: id,
          &quot;title&quot;: title,
          &quot;article&quot;: article,
          &quot;author&quot;: author,
          &quot;createdAt&quot;: createdAt.toIso8601String(),
          &quot;updatedAt&quot;: updatedAt.toIso8601String(),
        };
  }
  
  </code></pre>
  <p>Error</p>
  <pre><code>Restarted application in 1,226ms.
  flutter: 200
  flutter: [{_id: 60bcd42bd8fb4d2dabca24af, title: test article, article: {&quot;insert&quot;:&quot;This is some test file\n\ncode block 1&quot;},{&quot;insert&quot;:&quot;\n&quot;,&quot;attributes&quot;:{&quot;code-block&quot;:true}},{&quot;insert&quot;:&quot;quote block 1&quot;},{&quot;insert&quot;:&quot;\n&quot;,&quot;attributes&quot;:{&quot;blockquote&quot;:true}}&quot;, author: Ashutosh, createdAt: 2021-06-06T13:56:59.346Z, updatedAt: 2021-06-06T13:56:59.346Z, __v: 0}]
  flutter: type 'List&lt;dynamic&gt;' is not a subtype of type 'String'
  
  </code></pre>
  <p>I used <a href="https://app.quicktype.io/" rel="nofollow noreferrer">this</a> to generate the model class</p>