title: Passing constructor as argument in Flutter
body: <p>I have API communication service in my Flutter app with 10+ different services, and 100+ API calls that heed to parse data. In order to reuse code I've decided to create some common parsing code that is going to parse data from API: </p>
  
  <pre><code>ApiResponse handleObjectResponse({
      @required http.Response serverResponse,
      @required Function objectConstructor,
  }) {
      if (serverResponse.statusCode == 200) {
        dynamic responseObject = objectConstructor(json.decode(serverResponse.body));
        return ApiResponse(responseObject: responseObject);
      } else {
        ApiError error = responseHasError(serverResponse.body);
        return ApiResponse(error: error);
      }
  }
  </code></pre>
  
  <p>This way I am able to parse JSON object from API in a reusable way no matter what the Object class is, just by passing constructor function to this method. </p>
  
  <p>When I call this method in any of the Services I've created for fetching data like this: </p>
  
  <pre><code>handleObjectResponse(serverResponse: response, objectConstructor: ChartData.fromJson); 
  </code></pre>
  
  <p>I get error: <code>The getter 'fromJson' isn't defined for the class 'ChartData'.
  Try importing the library that defines 'fromJson', correcting the name to the name of an existing getter, or defining a getter or field named 'fromJson'.</code></p>
  
  <p>Where I think the problem is is in this model class and <code>factory</code> statement, but I don't know how to fix it: </p>
  
  <pre><code>class ChartData {
    List&lt;ChartDataPoint&gt; points;
  
    ChartData({
      this.points,
    });
  
    factory ChartData.fromJson(Map&lt;String, dynamic&gt; json) {
      List jsonPoints = json["data"];
      return ChartData(
          points: List.generate(jsonPoints.length,
          (i) =&gt; ChartDataPoint.fromJsonArray(jsonPoints[i])));
    }
  }
  </code></pre>