简体   繁体   中英

Unable to load asset (image) in flutter

This is how the code (app) looks like.

Aight people this is my first time doing this so I'll try to be precise and neat...

So I've just started learning flutter and I am watching a course on it, but while copying what the instructor was typing I stumbled upon on this problem whenever I try to run this code on my virtual or physical device it doesn't load the image. I've checked pubsec.yaml file and everything seems to be working fine but the image doesn't load. Btw I'm using AndroidStudio the latest version (canary I guess). Thanks in advance I hope someone kind will help me. Here is the code:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          title: Center(
            child: Text('I am rich.'),
          ),
          backgroundColor: Colors.blueGrey[900],
        ),
        body: Center(
          child: Image(
            image: AssetImage('diamond.png'),
          ),
        ),
      ),
    ),
  );
}

And here is a download link for the image in the code: https://drive.google.com/uc?export=download&id=1IA7othwL6jGtvc1Wfyj0K6hxrEH9OHam

When you have local files that you want to use within your code, you need to remember to add it into the pubspec.yaml file under the assets section. Here is an example of me registering a couple images over there:

flutter:
  ...
  assets:
    - diamond.png
    - gemstones/emerald.jpg

Please note that files (and folders) containing assets you wish to use should be registered in the root folder of your app and not your lib folder.

Did you put the path of your image in the pubspec.yaml ? For the Flutter to recognizes your asset image, you need to put the path of him in the pubspec.yaml file of your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM