I cant see my images in my flutter project - flutter

I did to make a simple pokedex application for trying myself. I completed my project and I did not get any issue in android studio.Other hand, When I started my application, I cant see images to get in my project in emulator. Can anyone help me for this issue .
Can look at this extension.
https://github.com/AlejandroTaichu/Pokedex.git
I want to see images in my project when I was opened my project.I am new on programming so I cant fix that.

You have forgotten extension of image in your code
child: Image.asset("images/${pokemon.name}")),
Change to below code
child: Image.asset("images/${pokemon.name}.png")),
Or add image extension(.png) in names of pokeList

You need to change the image name(path) according to the assets image path include the image type.
Next you need to create a future and call future on Future Builder widget.
late final future = callPokemon();
class _PokemonHomepageState extends State<PokemonHomepage> {
Future<List<Pokemons>> callPokemon() async {
var pokeList = <Pokemons>[];
var p1 = Pokemons(pokeId: 1, name: "Charmender.png", health: 100, power: 250);
var p2 = Pokemons(pokeId: 2, name: "squirtle.png", health: 150, power: 300);
var p3 = Pokemons(pokeId: 3, name: "balbazar.png", health: 300, power: 500);
pokeList.add(p1);
pokeList.add(p2);
pokeList.add(p3);
return pokeList;
}
late final future = callPokemon();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(title: const Text("POKEDEX"), backgroundColor: Colors.green),
body: FutureBuilder<List<Pokemons>>(
future: future,
builder: (context, snapshot) {
Made a pr on git, you can merge it

I suggest you to use flutter_gen (https://pub.dev/packages/flutter_gen) for assets. I recently found this package & its really helpful when using multiple images from assets.
With flutter_gen you can just setup few things one time & you can directly get image widgets from this package.
Setup flutter_gen
dart pub global activate flutter_gen
Add these runners in your dev dependencies
dev_dependencies:
build_runner:
flutter_gen_runner:
Setup path of generated assets file
flutter_gen:
output: lib/generated/
There are many options in this step. You can check their documentation.
Set image folder path
assets:
- assets/images/
Put your images in the path added
After doing these steps you just need to run
flutter packages pub run build_runner build
This file will generate an assets file name assets.gen.dart inside the lib/generated folder as we setup in step 3.
Note that you have to run this command whenever you change folder name or images to regerenated the assets.gen.dart
Thats it.
When you are done with these steps, you can directly get the images from this assets.gen.dart file like this
import 'package:flutter_boilerplate/generated/assets.gen.dart';
Padding(
padding: const EdgeInsets.all(20),
child: SizedBox(
height: 200, child: Assets.images.cat1.image(width: 200),
),
),
The plus benefit is that these assets provided widget support intellisense, so there is no chance for spelling mistake. Plus you directly get a widget from this with all the properties of default Image widget.
It also supports advance image types like svg, lottie, flare as well.

Related

unable to load Image in local packages

I am developing a local package for my flutter project, it's basically a UI library which includes a lot of image assets.
I don't want to load the assets in the package from my main project.
I want to load the assets in the package itself.
Here's what I did,
asset location: package_name/lib/assets
pubspec.yaml: packages/package_name/assets/bg.png
dart file: packages/package_name/assets/bg.png
Here are the step by step instructions to add image in Flutter:
Step 1: At the root of your project, create a new folder called assets.
Step 2: Inside the root folder, create another folder called images. You can give any name to this folder such as pictures, graphics, etc.
Step 3: Add your images inside the assets/images folder.
Step 4: Provide the image path in pubspec.yaml file as below.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets: <-- SEE HERE
- assets/images/lion.png
Step 5: Display image using the Image.asset() widget
u can use tihs idea
var files = Directory("${Directory.current.path.toString()}/lib/assets").listSync(); // this to create list of all file in the path
#override
void initState() {
// TODO: implement initState
print(files.toList()[0].path);// to see the alemint that u have and u //can creat list of all item and reach to them like this above
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: files.isNotEmpty ? Image.file(File('${files.toList() [0].path}'),fit: BoxFit.fill,) : Text(""),
),
),
);
}
do not forget add
import 'dart:io';

Flutter syncfusion_flutter_pdf package

I would like to show an image from firebase with syncfusion_flutter_pdf but I couldn't do it. I used exact same code block from official documentation but it didn't help me too.
The documentation gives the code below but there are no spesific way for Network image or something
Can anyone have an idea
Easiest way to use flutter printing package flutter printing
const imageProvider = const NetworkImage('https/image.png');
final image = await flutterImageProvider(imageProvider);
doc.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
})); // Page

How to set default size of macos app in flutter?

I am trying to build a macOS desktop app with flutter. I want the app to be full-width, edge-to-edge. However, when I run the app via the simulator, or after the build, it always launches the app with size 800x600.
I have set the height and width of the root container to double.infinity. In fact, even if I set the height and width to 10.0, it always launches the app with 800x600. I am new to flutter, so probably missing some fundamentals. Most tutorials I have come across talk about building a mobile app where this is never a problem because the app always launches to its full width.
Here is my entire test app code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.white),
height: double.infinity,
width: double.infinity,
child: Center(
child: Text(
'Hello World',
textDirection: TextDirection.ltr,
style: TextStyle(
fontSize: 32, fontWeight: FontWeight.bold, color: Colors.black),
),
),
);
}
}
There's now a plugin to do this, which is not a permanent thing as it is described as preliminary functionality before eventually being folded into the core libraries.
Using the plugin for now is still likely to be better than hard-coding directly modifying the native code, especially if you have multiple platforms you want to work on.
First add to the pubspec.yaml something like:
dependencies:
...
window_size:
git:
url: git://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
ref: 927f8cbc09b35d85245c095f2db8df9b186f6618
Using the specific Git reference to include this, as shown above, will give you good control over when you choose to pull updated code and make any changes this might entail.
You can then access various functions to set min/max window sizes, or frame, or get the current values, e.g.:
...
import 'dart:io'
import 'package:window_size/window_size.dart';
...
void main() {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
setWindowTitle("My Desktop App");
setWindowMinSize(Size(375, 750));
setWindowMaxSize(Size(600, 1000));
}
runApp(MyApp());
}
I hope this helps someone. I'll try and update this post when the real answer comes out. It seems likely that the interface will approximate what is presented in this library, but the feature set is likely to undergo some change.
Currently the only way to control the initial size is in native code (follow these issues: 1 and 2 to find out when that changes). You'd most likely want to set it in macos/Runner/MainFlutterWindow.swift.
It's not clear from your description whether you want to launch into full-screen mode, or just have a standard window the size of the client area of the screen; the code involved would be different depending on which you are trying to accomplish.
This package can help with it.
Size size = await DesktopWindow.getWindowSize();
print(size);
await DesktopWindow.setWindowSize(Size(500,500));
await DesktopWindow.setMinWindowSize(Size(400,400));
await DesktopWindow.setMaxWindowSize(Size(800,800));
await DesktopWindow.resetMaxWindowSize();
await DesktopWindow.toggleFullScreen();
bool isFullScreen = await DesktopWindow.getFullScreen();
await DesktopWindow.setFullScreen(true);
await DesktopWindow.setFullScreen(false);
I am not sure if this 100% valid, but I was looking for possibility to set window size. I have found package as #karora mentioned, but I wanted to only set window size and move on. So we can make it using xcode.
In project folder open Runner.xcodeproj:
macos -> Runner.xcodeproj
Then in Xcode project find MainMenu.xib then you can resize your flutter window.
You should be able to achieve this now within Dart code by using the window_size plugin.
The code to set initial window size could be something like:
#override
void initState() {
super.initState();
setWindowFrame(Rect.fromLTRB(1200.0, 500.0, 1800.0, 1125.0));
}
somewhere like your top-level stateful widget.
Though I should note that at the moment for me on Linux, the window frame sizing works, but positioning doesn't.
i need set default size for windows desktop app & this solution worked for me and cover the ios and linux platforms.
import 'package:desktop_window/desktop_window.dart'as window_size;
import 'dart:io';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
window_size.DesktopWindow.setMinWindowSize(Size(375, 750));
window_size.DesktopWindow.setMaxWindowSize(Size(600, 1000));
}
runApp(MyApp());
}

How to add 100+ manual images in pubspec.yaml in a easy way?

I need to add quite a lot of images in my app. And plan to keep the app offline. And writing 'assets/image1.jpg' is painful to the mind. I'm new to flutter and i don't know if there is any other way. Is there any?
To include an asset:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
To include all assets under a directory, specify the directory name with the / character at the end:
flutter:
assets:
- assets/
Note that only files located directly in the directory will be included; to add files located in subdirectories, create an entry per directory.
From Flutter Docs: (https://flutter.dev/docs/development/ui/assets-and-images)
Here is a flutter development tool named Flr(Flutter-R) can help you to auto specify image/text/font assets in pubspec.yaml and generate r.g.dart file. Then you can apply the asset in code by referencing it's asset ID function, such as:
import 'package:flutter_r_demo/r.g.dart';
// test_sameName.png
var normalImageWidget = Image(
width: 200,
height: 120,
image: R.image.test_sameName(),
);
// test_sameName.gif
var gifImageWidget = Image(
image: R.mage.test_sameName_gif(),
);
// test.svg
var svgImageWidget = Image(
width: 100,
height: 100,
image: R.svg.test(width: 100, height: 100),
);
// test.json
var jsonString = await R.text.test_json();
// test.yaml
var yamlString = await R.text.test_yaml();
// Amiri Font Style
var amiriTextStyle = TextStyle(fontFamily: R.fontFamily.amiri);
Up to now, Flr has supported Android Studio Plugin, CLI, and VSCode Extension:
Flr Android Studio Plugin Version
GitHub: https://github.com/Fly-Mix/flr-as-plugin
Plugin Homepage: https://plugins.jetbrains.com/plugin/13789-flr
flr-as-plugin Usage Example Gif:
Flr CLI Version
GitHub: GitHub: https://github.com/Fly-Mix/flr-cli
Plugin Homepage: https://rubygems.org/gems/flr
flr-cli Usage Example Gif:
Flr VSCode Extension Version
GitHub: GitHub: https://github.com/Fly-Mix/flr-vscode-extension
Extension Homepage: https://marketplace.visualstudio.com/items?itemName=LincolnLaw.flr
flr-vscode-extension Usage Example:
Now you can install any version Flr tool based on your development environment, and then easily add a lot of images/texts/fonts, enjoy~

How to add image in Flutter

I am developing Flutter app for the first time. I have an issue adding an image. I have a following questions:
Where to create an images folder?
Where to add assets tag in pubspec.ymal?
Is there any assets folder needed for this?
What I tried:
assets:
- images/lake.jpg
inside pubspec.ymal :
Full file :
name: my_flutter_app
description: A new Flutter application.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true,
assets:
- images/lake.jpg
Error log :
#/properties/flutter/properties/uses-material-design: type: wanted [boolean] got true,
Error detected in pubspec.yaml:
Error building assets
FAILURE: Build failed with an exception.
* Where:
Script '/home/abc/Downloads/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 435
* What went wrong:
Execution failed for task ':app:flutterBuildDebug'.
> Process 'command '/home/abc/Downloads/flutter/bin/flutter'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
Finished with error: Gradle build failed: 1
My main.dart code :
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
// Uncomment lines 7 and 10 to view the visual layout at runtime.
//import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
void main() {
//debugPaintSizeEnabled = true;
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
Widget titleSection = new Container(
padding: const EdgeInsets.all(32.0),
child: new Row(
children: [
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
new Container(
padding: const EdgeInsets.only(bottom: 8.0),
child: new Text(
'Oeschinen Lake Campground',
style: new TextStyle(
fontWeight: FontWeight.bold,
),
),
),
new Text(
'Kandersteg, Switzerland',
style: new TextStyle(
color: Colors.grey[500],
),
),
],
),
),
new Icon(
Icons.star,
color: Colors.red[500],
),
new Text('41'),
],
),
);
Column buildButtonColumn(IconData icon, String label) {
Color color = Theme.of(context).primaryColor;
return new Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Icon(icon, color: color),
new Container(
margin: const EdgeInsets.only(top: 8.0),
child: new Text(
label,
style: new TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.w400,
color: color,
),
),
),
],
);
}
Widget buttonSection = new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildButtonColumn(Icons.call, 'CALL'),
buildButtonColumn(Icons.near_me, 'ROUTE'),
buildButtonColumn(Icons.share, 'SHARE'),
],
),
);
Widget textSection = new Container(
padding: const EdgeInsets.all(32.0),
child: new Text(
'''
Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese Alps. Situated 1,578 meters above sea level, it is one of the larger Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour walk through pastures and pine forest, leads you to the lake, which warms to 20 degrees Celsius in the summer. Activities enjoyed here include rowing, and riding the summer toboggan run.
''',
softWrap: true,
),
);
return new MaterialApp(
title: 'Flutter Demo',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Top Lakes'),
),
body: new ListView(
children: [
new Image.asset(
'images/lake.jpg',
width: 600.0,
height: 240.0,
fit: BoxFit.cover,
),
titleSection,
buttonSection,
textSection,
],
),
),
);
}
}
I am referring to this tutorial https://flutter.io/tutorials/layout/
Also, I want to ask if there are any tools for a clean rebuild in a flutter as I can't find any options for this.
How to include images in your app
1. Create an assets/images folder
This should be located in the root of your project, in the same folder as your pubspec.yaml file.
In Android Studio you can right click in the Project view
You don't have to call it assets or images. You don't even need to make images a subfolder. Whatever name you use, though, is what you will register in the pubspec.yaml file.
2. Add your image to the new folder
You can just copy your image into assets/images. The relative path of lake.jpg, for example, would be assets/images/lake.jpg.
3. Register the assets folder in pubspec.yaml
Open the pubspec.yaml file that is in the root of your project.
Add an assets subsection to the flutter section like this:
flutter:
assets:
- assets/images/lake.jpg
If you have multiple images that you want to include then you can leave off the file name and just use the directory name (include the final /):
flutter:
assets:
- assets/images/
Note: Make sure to use the exact number of spaces. That's 2 spaces per indentation.
4. Use the image in code
Get the asset in an Image widget with Image.asset('assets/images/lake.jpg').
The entire main.dart file 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 often need to completely stop my app and restart it again, especially when adding assets. Otherwise I get a crash.
Running the app now you should have something like this:
Further reading
See the documentation for how to do things like provide alternate images for different densities.
Videos
The first video here goes into a lot of detail about how to include images in your app. The second video covers more about how to adjust how they look.
Flutter Tutorial - 1/2 Image - Deep Dive
Flutter Tutorial - 2/2 Image - Deep Dive
I think the error is caused by the redundant ,
flutter:
uses-material-design: true, # <<< redundant , at the end of the line
assets:
- images/lake.jpg
I'd also suggest to create an assets folder in the directory that contains the pubspec.yaml file and move images there and use
flutter:
uses-material-design: true
assets:
- assets/images/lake.jpg
The assets directory will get some additional IDE support that you won't have if you put assets somewhere else.
Create images folder in root level of your project.
Drop your image in this folder, it should look like
Go to your pubspec.yaml file, add assets header and pay close attention to all the spaces.
flutter:
uses-material-design: true
# add this
assets:
- images/profile.jpg
Tap on Packages get at the top right corner of the IDE.
Now you can use your image anywhere using
Image.asset("images/profile.jpg")
The problem is in your pubspec.yaml, here you need to delete the last comma.
uses-material-design: true,
To use image in Flutter. Do these steps.
1. Create a Directory inside assets folder named images. As shown in figure below
2. Put your desired images to images folder.
3. Open pubpsec.yaml file . And add declare your images.Like:--
4. Use this images in your code as.
Card(
elevation: 10,
child: Container(
decoration: BoxDecoration(
color: Colors.orangeAccent,
image: DecorationImage(
image: AssetImage("assets/images/dropbox.png"),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
child: Text("$index",style: TextStyle(color: Colors.red,fontSize: 16,fontFamily:'LangerReguler')),
alignment: Alignment.center,
),
);
An alternative way to put images in your app (for me it just worked that way):
1 - Create an assets/images folder
2 - Add your image to the new folder
3 - Register the assets folder in pubspec.yaml
4 - Use this code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
var assetsImage = new AssetImage('assets/images/mountain.jpg'); //<- Creates an object that fetches an image.
var image = new Image(image: assetsImage, fit: BoxFit.cover); //<- Creates a widget that displays an image.
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Climb your mountain!"),
backgroundColor: Colors.amber[600], //<- background color to combine with the picture :-)
),
body: Container(child: image), //<- place where the image appears
),
);
}
}
Create your assets directory the same as lib level
like this
projectName
-android
-ios
-lib
-assets
-pubspec.yaml
then your pubspec.yaml like
flutter:
assets:
- assets/images/
now you can use Image.asset("/assets/images/")
--> Follow Below Steps One OR Multiple Image Insertion.:
-> Create 'assets/images' folder as in project module.
-> put images which you want.
-> use of image using this syntax. ex.
- Image.asset('assets/images/tony.jpg')
-> use image avatar which you want like, circle, square and much more as your need.
-> Open 'pubspec.yaml' file
-> write the code in 'flutter:' section. ex.
-------------------------------
uses-material-design: true
assets:
- assets/images/ // if multiple images you have then
- assets/images/imagename.extension // if single images you have then
-------------------------------
-> click on 'PUB GET' button which occurs on Right side of Screen above.
-> Run App.
-> if you get any issue then
-> Go to file -> Invalid caches/Restart -> Invalid Caches/Restart button
-> Done.
See Below Images to Get a Better idea of Implementation.
•• Here, add image file like below. ••
•• Here, add image file Description in PUBSPEC.YAML file like below. ••
Done.☻♥
When you adding assets directory in pubspec.yaml file give more attention in to spaces
this is wrong
flutter:
assets:
- assets/images/lake.jpg
This is the correct way,
flutter:
assets:
- assets/images/
their is no need to create asset directory and under it images directory and then you put image.
Better is to just create Images directory inside your project where pubspec.yaml exist and put images inside it
and access that images just like as shown in tutorial/documention
assets:
- images/lake.jpg // inside pubspec.yaml
If the image is inside a package dependency, you should also provide the package name, event if you are referencing the image from the same dependency!
Image.asset("assets/pics/events_empty.png", package: "ui_elements"),
Ref:
Asset images in package dependencies
Make sure you create the assets folder in the main directory, not in the lib folder.
you can rather use this to see access to everything inside assets, because there may be folders like images, logo, icon, etc...
create the assets folder in the root of the project
MyProject
android
assets
ios
lib
test
web
windows
.gitignore
pubspec.yaml
and in your pubspec.yaml, there are
assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
replace the last two lines by the 'assets/' to be able to directly access everything inside assets
if you have directly images in assets
assets:
- assets/
or if you have sub folders in assets
assets:
- assets/folder1/
- assets/folder2/
- assets/folder3/
Create an assets/images folder and Add your image to the folder
Register the assets folder in pubspec.yaml
flutter:
assets:
- assets/images/placeholder_circle_image.png
If you have multiple images
flutter:
assets:
- assets/images/
Use the image in code
Image.asset(
'assets/images/item_entry/add_car_video.png',
width: 100,
height: 100,
fit: BoxFit.cover,
cacheWidth:100 * MediaQuery.of(context).devicePixelRatio.round(),
),
Error when comma is at the end of below line
uses-material-design: true,
when the line after assets: indent is not correct it throws below error
If the indent is corrected as shown in below, then error is fixed.
If the asset file is not added then it highlights that with yellowish background but Pub get doesn't throw error but there will be an error during installation.
In short, Code Indent and syntax is important
You can create a folder and reference it as shown