Skip to content

Correct error handling in custom ByteLoader #1080

@escamoteur

Description

@escamoteur

Hi @dnfield,

first of all, I'm thrilled by the elegance of your internal design. so it was pretty easy to add an SvgCachedNetworkLoader

class SvgCachedNetworkLoader extends SvgLoader<File> {
  /// See class doc.
  const SvgCachedNetworkLoader(
    this.url, {
    this.headers,
    super.theme,
    super.colorMapper,
  });

  /// The [Uri] encoded resource address.
  final String url;

  /// Optional HTTP headers to send as part of the request.
  final Map<String, String>? headers;

  @override
  Future<File> prepareMessage(BuildContext? context) async {
    return await _cacheManager.getSingleFile(url, headers: headers);
  }

  @override
  String provideSvg(File? message) {
    final Uint8List bytes = message!.readAsBytesSync();
    return utf8.decode(bytes, allowMalformed: true);
  }

  @override
  int get hashCode => Object.hash(url, headers, theme, colorMapper);

  @override
  bool operator ==(Object other) {
    return other is SvgCachedNetworkLoader &&
        other.url == url &&
        other.headers == headers &&
        other.theme == theme &&
        other.colorMapper == colorMapper;
  }

  @override
  String toString() => 'SvgCachedNetworkLoader($url)';
}

Besides that I find it amazing that it is possible to pass a File to compute I wasn't sure if I need to do any special error handling in case prepareMessage or provideSvg throws an exception or if that is totally ok if they do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions