AssetImage is not displaying image in flutter app - dart

image not displaying in flutter app.
But I got some errors in debug console.
I/FlutterActivityDelegate(22603): onResume setting current activity to this
I/FlutterActivityDelegate(22603): onResume setting current activity to this
I/Timeline(22603): Timeline: Activity_idle id: android.os.BinderProxy#3eb59326 time:39937973
I/flutter (22603): ══╡ EXCEPTION CAUGHT BY SERVICES ╞══════════════════════════════════════════════════════════════════
I/flutter (22603): The following assertion was thrown resolving an image codec:
I/flutter (22603): Unable to load asset: assets/images/logo.png
I/flutter (22603): When the exception was thrown, this was the stack:
I/flutter (22603): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (22603): <asynchronous suspension>
I/flutter (22603): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:383:44)
I/flutter (22603): <asynchronous suspension>
I/flutter (22603): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:368:14)
I/flutter (22603): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:265:86)
I/flutter (22603): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:82:22)
I/flutter (22603): #5 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:265:63)
I/flutter (22603): (elided 8 frames from package dart:async)
I/flutter (22603): Image provider: AssetImage(bundle: null, name: "assets/images/logo.png")
I/flutter (22603): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#19ce7(), name: "assets/images/logo.png",
I/flutter (22603): scale: 1.0)
I/flutter (22603): ════════════════════════════════════════════════════════════════════════════════════════════════════
D/ViewRootImpl(22603): ViewPostImeInputStage ACTION_DOWN
D/ViewRootImpl(22603): ViewPostImeInputStage ACTION_DOWN
My Pubspec.yaml file
assets:
- assets/images/logo.png
login.dart code
new Image.asset("assets/images/logo.png", width: 60.0,
height: 24.0,
fit: BoxFit.cover)
directory structure
pubspec.yaml file

i mentioned images files in wrong way. i put space between '-' and image name instead of tab.
assets:
- assets/images/logo.png
Don't put spaces between the character instead of tab in pubspec.yaml file

There may be two issues:
1.) Either you pubspec.yaml file is not having proper indention. Attaching snippet for reference.
flutter:
uses-material-design: true
assets:
- assets/
- assets/ will consider all the images in the directory.
2.) If you are using .jpg image then please change it to .jpeg wherever you are calling it.
Attaching the snippet for you reference
class _UserLoginState extends State<UserLogin> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Image(image: AssetImage("assets/christmas.jpeg"),
fit: BoxFit.cover,
],
)
);
}
}

Make sure the folder you are referring in Image.asset contains the file.
For example:
Image.asset(
"./assets/images/loading.gif",
height: 125.0,
width: 125.0,
)
The folder should be:
C:\your_app_folder\assets\images
pubspec.yaml:
assets:
- assets/
- assets/images/
Run flutter clean
to clean the intermediate files and refresh.

Hot Reload was the issue in my case. I simply restarted the Android studio and re-run the app, it all worked!.

I assume you copied your files inside assets/images/ folder right?
Also you need to reference your images into the pubspec.yaml file.
flutter:
...
assets:
- assets/images/logo.png

Make sure your image folder is in project folder.

Refer https://api.flutter.dev/flutter/painting/AssetImage-class.html
Add assets images in pubspec.yaml file and in the asset mention the path from images and it works fine.
new AssetImage("images/logo.png")
For example
CircleAvatar(
radius: 80,
backgroundImage: AssetImage('images/logo.png'),
),

I just replaced
Image.asset("assets\images\_Lightweight.jpeg"),
To
Image.asset("assets/images/_Lightweight.jpeg"),

my problem was indentation of assets section. I wrote it at the beginning of the line, whereas it should be indented with a Tab after flutter: section.
Robie

I faced the same issue after adding a new image to the asset folder. I was doing "Hot Reload", I destroyed the app from the background, re-run the app and the problem got resolved.

Your pubspec.yaml is not intended properly
flutter:
uses-material-design: true
assets:
- images/
Check if it's intended this way.

There are several opportunities to make the mistake, higher chance that it's in the pubspec.yaml declaration.
Most of the time it's better to specify the directory of your assets. This way you'll edit pubspec.yaml less frequently.
The asset directory/item is not declared properly (e.g. wrong indent)
If your IDE is Android Studio and you edited the pubspec, hitting the Run button won't cut it. Execute flutter run in the Terminal to run the app.
If you can't type in the Terminal, flutter may already be running. Press ctrl + c to terminate the process then run. No need to restart your IDE.

These changings will definately work.
AssetImage('mypicture.jpg')
Above is my code, I have not used "assets" folder name with my image file name and it works perfect.
and these are my assets in 'pubspec.yaml' file.
assets:
- assets/cat.png
- assets/mypicture.jpg```

Flutter doesn't support jpg files.
Try the complete path of the file with '/'(forward slash).

Related

Expected a key while parsing a block mapping

Error detected in pubspec.yaml:
Error on line 48, column 4: Expected a key while parsing a block mapping.
pubspec.yaml:
flutter:
uses-material-design: true
assets:
- images/googlelogo.png
As you mentioned yourself in the comment, indentation is important in pubspec.yaml file. It should be like this:
flutter:
uses-material-design: true
assets:
- images/googlelogo.png

flutter unable to load svg resource

I am using the flutter_svg (version 0.10.3)package to load svg in my app. but it shows me an error saying that it is unable to load the resource.
here is my code
static final Map<Gender, String> _genderImage = {
Gender.female: 'assets/images/gender_female.svg',
Gender.other: 'assets/images/gender_other.svg',
Gender.male: 'assets/images/gender_male.svg'
};
Widget icon = Padding(
padding: EdgeInsets.only(left: _leftPadding(context)),
child: SvgPicture.asset(
_assetName,
height: _iconSize(context),
width: _iconSize(context),
));
this is the error:
Another exception was thrown: Unable to load asset: assets/images/gender_male.svg
Adding your assets to the pubspec.yaml is necessary, because Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.

Image loading showing error: EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE

I am loading image from URL for my circle avatar and it is showing me following error:
[VERBOSE-2:codec.cc(97)] Failed decoding image. Data is either invalid, or it is encoded using an unsupported format.
flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following _Exception was thrown resolving an image codec:
flutter: Exception: operation failed
Here is my code for this:
CircleAvatar(
backgroundColor: blueColor,
radius: 45,
child: new Text( strFName.substring(0, 1) + strLName.substring(0, 1) style: styleCircleName,),
backgroundImage: NetworkImage(strImageURL),
)
The error Failed decoding image. Data is either invalid, or it is encoded using an unsupported format. is usually caused by trying to display an invalid image. An image could possibly be invalid if it's in an incorrect format i.e. trying to display an "incomplete" base64 image, path to the image file/asset being incorrect.
The image formats supported by Flutter are listed here. Some of them are JPEG, PNG, GIF, Animated GIF, WebP, Animated WebP, BMP, and WBMP.

Flutter: How to Change tooltip name of "paste" on textField to device's language

When i go to copy or paste on textfield, the tooltip is always on english language, and my device is on Pt-Br. How can i change or fix that ?
Add flutter_localizations to your pubspec.yaml:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
In your MaterialApp:
return MaterialApp(
...
locale: const Locale('de'), // change to locale you want. not all locales are supported
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
);

The named parameter 'child' isn't defined. in Center() constructor

I ran flutter upgrade today, and now I am getting an error that says-
[dart] The named parameter 'child' isn't defined.
The project is newly created and the default code is untouched, but it still has the same Error:
Clean the project cache by running
flutter clean cache
Then invalidate caches / restart Android Studio or VS Code.
Try Restarting your Analysis Dart Server.
At the bottom of Android Studio click on the Dart Analysis tab
Click on the Restart icon.
In my case it happens when I name the widget with the same name of a flutter component, like so:
class OutlineButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return OutlineButton(
child: Text('+R\$ 5'),
onPressed: () {},
borderSide: BorderSide(color: Colors.grey),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
);
}
}
You need to change the name of the created component with a different name, for example:
class CustomOutlineButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return OutlineButton(
child: Text('+R\$ 5'),
onPressed: () {},
borderSide: BorderSide(color: Colors.grey),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
);
}
}
Go to file Settings
Search Flutter (in the search bar)
Provide Flutter SDK path
Actually, I reinstalled the flutter SDK to solve the problem. After a few days, the same error occurred, but then I Started hovering over the red line, after a minute, the error was solved automatically. I guess the SDK connects with internet to check for all the libraries and syntax stuff once we open the IDE.
It happens because of the old cache. Just clean the cache with the following command
flutter clean cache
I just had the same issue. It looks like I changed a flutter lib file by mistake.
To fix this, without reinstalling Flutter SDK:
go to your Flutter SDK installation path
open terminal and type: git status
it will show the modified files in red (ex: modified: packages/flutter/lib/src/widgets/basic.dart
revert modifications file using: git checkout FILE_PATH (ex: git checkout packages/flutter/lib/src/widgets/basic.dart)
The solution is -
Change the name of the class in which you are using the Center widget because, according to the standards of flutter you cannot write the name of the class same as the widget you are using inside that class. If you do so, then you cannot be able to make child or children inside that widget.
Also, the solution of Felipe Augusto is correct.

Resources