close

How to add image in Flutter

This is a repost of an answer I composed on Stack Overflow.

Image for post

I initially composed this answer when I was figuring out how to add pictures to my Flutter venture. Look at the documentation, yet in the event that you need somewhat more bit by bit help, continue perusing.

1. Create an assets/images folder

Image for post
assets/images folder
  • This should be situated in the foundation of your venture, in a similar organizer as your pubspec.yaml record.
  • In Android Studio you can right snap in the Project see
  • You don’t need to call it resources or pictures. You don’t have to make pictures a subfolder. Whatever name you use, however, is the thing that you will enroll in the pubspec.yaml record.

2. Add your picture to the new envelope

  • You can simply duplicate your picture into resources/pictures. The general way of lake.jpg, for instance, would be resources/pictures/lake.jpg.

3. Register the resources organizer in pubspec.yaml

  • Open the pubspec.yaml document that is in the foundation of your task.
  • Add a resources subsection to the flutter segment this way:
flutter:   
  assets:     
    - assets/images/lake.jpg
  • In the event that you have different pictures that you need to incorporate, at that point you can leave off the document name and simply utilize the catalog name (incorporate the last/):
flutter:
   assets:
     - assets/images/

4. Use the image in code

  • Get the resource in an Image gadget with Image.asset(‘assets/pictures/lake.jpg’).
  • The whole main.dart document is here:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Image from assets"),
        ),
        body: Image.asset('assets/images/lake.jpg'), //   <-- image
      ),
    );
  }
}

5. Restart your app

When making changes to pubspec.yaml I find that I frequently need to totally stop my application and restart it once more, particularly while adding resources. Else I get an accident.

Running the application now you ought to have something like this:

Image for post

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment