read method

  1. @override
Future<String> read()
override

Implementation

@override
Future<String> read() async {
  if (pathOrUrl.startsWith("http")) {
    var prefs = await SharedPreferences.getInstance();
    Map<String, String> headers = {
      'If-Modified-Since': prefs.getString(pathOrUrl + '_Last-Modified') ?? ''
    };
    var jwt = prefs.getString("jwt") ?? prefs.getString("idtoken") ?? '';
    var sid = prefs.getString("sid") ?? '';
    var q = sid.isEmpty ? '' : '?sid=$sid';
    headers.addAll({'Authorization': 'Bearer $jwt'});
    var response = await http.get(Uri.parse(pathOrUrl + q), headers: headers);
    if (response.statusCode > 299) {
      if (response.statusCode == 304) {
        return prefs.getString(pathOrUrl) ?? '';
      }
      return "fun build(){return Center(child: Text(\"${response.body}\"));}";
    }
    if (response.headers['last-modified'] != null) {
      prefs.setString(pathOrUrl, response.body);
      prefs.setString(
          pathOrUrl + '_Last-Modified', response.headers['last-modified']!);
    }
    return response.body;
  }
  return Future.value(
      "fun build(){return Center(child: Text(\"invalid url\"));}");
}