fetchExtract function

Future<(WikiResult?, String?)> fetchExtract(
  1. String title
)

Returns the introduction in plain text for the Wikipedia page with the specified title (WikiResult, null) or (null, errorMessage)

Implementation

Future<(WikiResult?, String?)> fetchExtract(String title) async {
  final response = await http
      .get(Uri.parse('https://en.wikipedia.org/w/api.php?action=query&redirects&prop=extracts|pageimages&titles=${const HtmlEscape().convert(title)}&exintro=true&exsentences=2&explaintext=true&pithumbsize=240&format=json'));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response, then parse the JSON.
    _log.fine(jsonDecode(response.body));

    try {
      return (WikiResult.fromJson(jsonDecode(response.body) as Map<String, dynamic>), null);

    } catch (e) {
      _log.fine('error parsing JSON: $e');
      return (null, 'Error: $e');
    }
  } else {
    // If the server did not return a 200 OK response,
      return (null, 'Error: ${response.statusCode}');
  }
}