Ashutosh Aswal
Ashutosh Aswal

Reputation: 575

Unable to load asset: images/L.jpg

[Main Issue] => At build time I am getting this error but it goes after hot reload.

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: images/L.jpg

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:664:31)
#2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:648:14)
#3      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:501:13)
...
Image provider: AssetImage(bundle: null, name: "images/L.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#dcad6(), name: "images/L.jpg", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          backgroundColor: Colors.teal,
          appBar: AppBar(
            title: Center(child: Text('Mi Card')),
            backgroundColor: Colors.teal[300],
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CircleAvatar(
                backgroundImage: AssetImage('images/L.jpg'),
                radius: 50.0,
              ),
              Text(
                'Jack Lee',
                style: TextStyle(
                  fontFamily: 'Pacifico',
                  fontSize: 40.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
              Text(
                'FLUTTER DEVELOPER',
                style: TextStyle(
                  fontFamily: 'Source Sans Pro',
                  fontSize: 13,
                  color: Colors.white,
                  letterSpacing: 10.0,
                ),
              ),
              SizedBox(
                height: 10,
                width: 300,
                child: Divider(
                  color: Colors.white,
                ),
              ),
              Card(
                margin: EdgeInsets.all(20),
                child: ListTile(
                  leading: Icon(
                    Icons.phone,
                    color: Colors.teal,
                  ),
                  trailing: Padding(
                    padding: const EdgeInsets.symmetric(
                        vertical: 10, horizontal: 20),
                    child: Text(
                      '+91-1234567890',
                      style: TextStyle(
                        fontFamily: 'Source Sans Pro',
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                        color: Colors.teal,
                        letterSpacing: 8,
                      ),
                    ),
                  ),
                ),
              ),
              Card(
                margin: EdgeInsets.symmetric(vertical: 5, horizontal: 20),
                child: ListTile(
                  leading: Icon(
                    Icons.email,
                    color: Colors.teal,
                  ),
                  trailing: Padding(
                    padding: const EdgeInsets.symmetric(
                        vertical: 10, horizontal: 20),
                    child: Text(
                      '[email protected]',
                      style: TextStyle(
                        fontFamily: 'Source Sans Pro',
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                        color: Colors.teal,
                        letterSpacing: 1.0,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

pubspec.yaml

name: Mi_Card
description: A new Flutter application.
version: 1.0.0+1
environment:
  sdk: '>=2.7.0 <3.0.0'
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.3
dev_dependencies:
  flutter_test:
    sdk: flutter
flutter:
  uses-material-design: true
  assets:
    - images/L.jpg
  fonts:
    - family: Pacifico
      fonts:
        - asset: fonts/Pacifico-Regular.ttf
    - family: Source Sans Pro
      fonts:
        - asset: fonts/SourceSansPro-Regular.ttf

[Secondary Issue] => Also my app is working fine with emulator but it is getting lots of error while I am trying to run it on a physical device.*

Upvotes: 0

Views: 1013

Answers (1)

Mr vd
Mr vd

Reputation: 988

Use this steps

  1. Create images folder in your application
  2. Add image in this folder
  3. Add your path in pubspec.yaml

assets:

- images/folder.png
  1. Run flutter get
  2. Hot restart your App

Upvotes: 3

Related Questions