Last active
May 11, 2023 14:56
-
-
Save cdmunoz/ec8d1db4da668f8085d3efe27e70a60f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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