Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct error handling in custom ByteLoader #1080

Open
escamoteur opened this issue Jun 12, 2024 · 0 comments
Open

Correct error handling in custom ByteLoader #1080

escamoteur opened this issue Jun 12, 2024 · 0 comments

Comments

@escamoteur
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant