load image from asset flutter code example

Example 1: flutter image load

Image.asset(name) is essentially Image(image: AssetImage(name)),
Image.file(path) is essentially Image(image: FileImage(File(path))),
Image.network(url) is essentially Image(image: NetworkImage(url)),
Image.memory(list) is essentially Image(image: MemoryImage(list))

Example 2: flutter get image file from assets

import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';

Future<File> getImageFileFromAssets(String path) async {
  final byteData = await rootBundle.load('assets/$path');

  final file = File('${(await getTemporaryDirectory()).path}/$path');
  await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

  return file;
}

File f = await getImageFileFromAssets('images/myImage.jpg');

Tags:

Misc Example