Kinda Code
Home/Flutter/Flutter: Reading Bytes from a Network Image

Flutter: Reading Bytes from a Network Image

Last updated: August 19, 2022

The example below shows you how to read bytes from a network image in Flutter.

The code:

// main.dart
import 'dart:typed_data';
import 'package:flutter/services.dart';

// Reading bytes from a network image
Future<Int8List> readNetworkImage(String imageUrl) async {
  final ByteData data =
      await NetworkAssetBundle(Uri.parse(imageUrl)).load(imageUrl);
  final Int8List bytes = data.buffer.asInt8List();
  return bytes;
}

void main() async {
  const String imageUrl =
      'https://www.kindacode.com/wp-content/uploads/2021/08/food.jpeg';

  final bytes = await readNetworkImage(imageUrl);
  print(bytes);
}

Output:

[-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, -1, -37, 0, 67, 0, 5, 3, 4, 4, 4, 3, 5, 4, 4, 4, 5, 5, 5, 6, 7, 12, 8, 7, 7, 7, 7, 15, 11, 11, 9, 12, 17, 15, 18, 18, 17, 15, 17, 17, 19, 22, 28, 23, 19, 20, 26, 21, 17, 17, 24, 33, 24, 26, 29, 29, 31, 31, 31, 19, 23, 34, 36, 34, 30, 36, 28, 30, 31, 30, -1, -37, 0, 67, 1, 5, 5, 5, 7, 6, 7, 14, 8, 8, 14, 30, 20, 17, 20, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, -1, -62, 0, 17, 8, 1, -86, 2, -128, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 28, 0, 0, 0, 7, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 0, 8, -1, -60, 0, 26, 1, 0, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, -1, -38, 0, 12, 3, 1, 0, 2, 16, 3, 16, 0, 0, 1, -74, 3, 126, -78, -75, -54, 67, 0, 113, -52, 8, -107, -56, 3, 98, 46, 70, 33, -57, 1, -108, -61, -64, 28, 11<…>

The output is trimmed because it is too long. In case you want to see all of it, head to this article: How to Fully Print a Long String/List/Map in Flutter.

Further reading:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.