When trying to upload image on flutter it says "unable to load asset: the asset does not exist"

44 Views Asked by At

On flutter, when trying to upload an image on a window it says "unable to load asset: the asset does not exist".

The image is kept in a file named "assets" kept in the same folder as the rest of the program.

Here is my code

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
  home: Home()
));

class Home extends StatelessWidget {

@override
 Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
      title: Text('App Title'),
      titleTextStyle: TextStyle (
        fontSize: 25.0,
        fontWeight: FontWeight.bold,
        color: const Color.fromARGB(255, 248, 245, 245),
        fontFamily: 'Ubuntu',
    
      ) ,
      centerTitle: true,
      backgroundColor: Color.fromARGB(255, 235, 8, 4),
  
    ),
    body: Center(
      child: Image(
        image: AssetImage('assets/hotcoffee.jpg'),
    
      )
  
    ),
  floatingActionButton: FloatingActionButton(
    onPressed: () {},
    child: Text('click'),
    backgroundColor: Color.fromARGB(255, 235, 8, 4),
    ),


);

  }
}

here is my pubspec.yaml file:

name: flutter_application_2
escription: "A new Flutter project."
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=3.2.5 <4.0.0'

dependencies:
  flutter:
  sdk: flutter

dev_dependencies:
  flutter_test:
  sdk: flutter
flutter_lints: ^2.0.0

flutter:
   uses-material-design: true

 fonts:
  - family: Ubuntu
    fonts:
   - asset: assets/fonts/Ubuntu-Regular.ttf  


assets:
  - assets\hotcoffee.jpg

Tried multiple solutions. Nothing has worked.

Sorry for the unnecessary add on. Stack Overflow won't let me post the question unless I add more details.

2

There are 2 best solutions below

0
boze-noob On

I think that you did not add the right path for the image. It should be

assets:
  - assets/hotcoffee.jpg

With the "/" instead of the backslashe. Also considering your app will have multiple assets it is better to reference the assets folder and not the asset itself.

If I am wrong try to run flutter pub get or flutter clean?

0
Zohair On

It looks like there are a couple of issues in your code and pubspec.yaml file. Let's address them:

  1. In your pubspec.yaml file, use forward slashes (/) instead of backslashes () for the asset path:

    assets:

  • assets/hotcoffee.jpg
  1. Make sure your folder structure is correct. The hotcoffee.jpg file should be inside an assets folder in the root of your Flutter project.

    project_root |-- assets | |-- hotcoffee.jpg |-- lib | |-- main.dart |-- pubspec.yaml |-- ...

  2. After making these changes, run flutter pub get in the terminal to ensure that the changes are reflected in your project.

Ensure that the asset path in your Image widget is correct. It should be relative to the pubspec.yaml file:

  child: Image(
  image: AssetImage('assets/hotcoffee.jpg'),
)

With these changes, your image asset should be recognized and loaded properly. If the issue persists, try restarting your IDE or running flutter clean followed by flutter run.