title: How to encode an object to json in Flutter
body: <p>I am trying to convert the object "Week" to json. </p>
  
  <p><a href="https://flutter.dev/docs/development/data-and-backend/json" rel="noreferrer">https://flutter.dev/docs/development/data-and-backend/json</a> this is the source that i used</p>
  
  <pre><code>class Week{
    DateTime _startDate;
    DateTime _endDate;
    List&lt;Goal&gt; _goalList;
    String _improvement;
  
    Week(this._startDate, this._endDate){
      this._goalList = List&lt;Goal&gt;();
      this._improvement = "";
    }
  
    Week.fromJson(Map&lt;String, dynamic&gt; json)
      : _startDate =  json['startDate'],
        _endDate = json['endDate'],
        _goalList = json['goalList'],
        _improvement = json['improvement'];
  
    Map&lt;String, dynamic&gt; toJson() =&gt; 
    {
      'startDate': _startDate,
      'endDate': _endDate,
      'goalList': _goalList,
      'improvement': _improvement,
    };
  }
  </code></pre>
  
  <p>I used this: </p>
  
  <pre><code>DateTime startDate = currentDate.subtract(new Duration(days:(weekday-1)));
  DateTime endDate = currentDate.add(new Duration(days:(7-weekday)));
  
  Week week = new Week(startDate, endDate);
  var json = jsonEncode(week);
  </code></pre>
  
  <p>But the problem is that I get this result:</p>
  
  <pre><code>Unhandled Exception: Converting object to an encodable object failed: Instance of 'Week'
  #0      _JsonStringifier.writeObject (dart:convert/json.dart:647:7)
  #1      _JsonStringStringifier.printOn (dart:convert/json.dart:834:17)
  #2      _JsonStringStringifier.stringify (dart:convert/json.dart:819:5)
  #3      JsonEncoder.convert (dart:convert/json.dart:255:30)
  #4      JsonCodec.encode (dart:convert/json.dart:166:45)
  #5      jsonEncode (dart:convert/json.dart:80:10)
  </code></pre>