asset image flutter code example

Example 1: flutter image asset

// pubspec.yaml
flutter:
  assets:
    - graphics/

// Inside your widget
Image(image: AssetImage('graphics/background.png'))

Example 2: flutter asset image not showing

flutter:  
  uses-material-design: true   
  assets:   
    - assets/

class _UserLoginState extends State<UserLogin> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Image(image: AssetImage("assets/christmas.jpeg"),
          fit: BoxFit.cover,
        ],
      )
    );
  }
}

Example 3: 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');