fetchComments method

Future<List<Comment>> fetchComments (
  1. {@required ParamsCommentList params}
)

This returns a list of Comment based on the filter parameters specified through ParamsCommentList object. By default it returns only ParamsCommentList.perPage number of comments in page ParamsCommentList.pageNum.

In case of an error, a WordPressError object is thrown.

Implementation

async.Future<List<Comment>> fetchComments(
    {@required ParamsCommentList params}) async {
  final StringBuffer url = new StringBuffer(_baseUrl + URL_COMMENTS);

  url.write(params.toString());

  final response = await http.get(url.toString(), headers: _urlHeader);

  if (response.statusCode >= 200 && response.statusCode < 300) {
    List<Comment> comments = new List<Comment>();
    final list = json.decode(response.body);
    list.forEach((comment) {
      comments.add(Comment.fromJson(comment));
    });
    return comments;
  } else {
    try {
      WordPressError err =
          WordPressError.fromJson(json.decode(response.body));
      throw err;
    } catch (e) {
      throw new WordPressError(message: response.body);
    }
  }
}