Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Last active May 11, 2023 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmunoz/ec8d1db4da668f8085d3efe27e70a60f to your computer and use it in GitHub Desktop.
Save cdmunoz/ec8d1db4da668f8085d3efe27e70a60f to your computer and use it in GitHub Desktop.
/// this class uses Isolates.run, only available after Dart 2.19, allowing in parallel obtaining a file in bytes
/// then answering to its caller without blocking the UI
/// remember: when using isolates, the functions they call must be top level or static
import 'dart:isolate';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
Future<http.Response> _fileResponse(Uri uri) async {
final response = await http.get(uri);
return response;
}
class Uint8ListParser {
Future<Uint8List> getAsUint8List(String url) async {
final uri = Uri.parse(url);
final response = await Isolate.run(() => _fileResponse(uri));
return response.bodyBytes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment