issues method Null safety

Stream<Issue> issues(
  1. String query,
  2. {String? sort,
  3. int pages = 2}
)

Search for issues and pull-requests using query. Since the Search Rate Limit is small, this is a best effort implementation. API docs: https://developer.github.com/v3/search/#search-issues

Implementation

Stream<Issue> issues(String query, {String? sort, int pages = 2}) {
  final params = <String, dynamic>{'q': query};
  if (sort != null) {
    params['sort'] = sort;
  }

  final controller = StreamController<Issue>();

  var isFirst = true;

  PaginationHelper(github)
      .fetchStreamed('GET', '/search/issues', params: params, pages: pages)
      .listen((response) {
    if (response.statusCode == 403 &&
        response.body.contains('rate limit') &&
        isFirst) {
      throw RateLimitHit(github);
    }

    isFirst = false;

    final input = jsonDecode(response.body);

    if (input['items'] == null) {
      return;
    }

    final items = input['items'] as List;

    items.map((item) => Issue.fromJson(item)).forEach(controller.add);
  }).onDone(controller.close);

  return controller.stream;
}