fetchImageAsUint8List function
Fetches an image from a network URL as a Uint8List.
This function allows you to fetch an image from a network URL and convert it into a Uint8List for further use.
Parameters:
imageUrl
: A String representing the network URL of the image to be fetched.
Returns: A Future that resolves to a Uint8List containing the image data.
Example Usage:
final Uint8List imageBytes = await fetchImageAsUint8List('https://example.com/image.jpg');
Implementation
Future<Uint8List> fetchImageAsUint8List(String imageUrl) async {
final response = await http.get(Uri.parse(imageUrl));
if (response.statusCode == 200) {
// Convert the response body to a Uint8List
final Uint8List uint8List = Uint8List.fromList(response.bodyBytes);
return uint8List;
} else {
throw Exception('Failed to load image: $imageUrl');
}
}