I am writing code for a simple flutter app.
The code is very simple,
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.teal,
shadowColor: Colors.black38,
title: Text(
'Some Ramdom Text',
style: TextStyle(color: Colors.black),
),
),
body: Center(
child: Image.asset('images/poor.png'),
),
),
),
);
}
However when I run it, I get the below error:
The following assertion was thrown resolving an image codec:
Unable to load asset: images/poor.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
What could I be missing? I tried refactoring the project.
I checked the pubspec.yaml, it looks good.
assets:
- images/
The Error states itself It was not able to find the assets or load it do a pub get in pubsec.yaml after saving it.
It will fix your problem
I am using Android Studio, and when I open pubspec.yaml, I see the option Pub Get on the right top corner on the screen.
This enables the project to install the referenced packages.
After this I was able to view the image.
Related
I am in the process of updating a Flutter project from v2 to v3 and running into some weird issues. Everywhere anything Material related is being used, i get this prop does not exist. Here are some examples:
_uiViewPort(MaterialApp(
home: StreamBuilder<bool>(
stream: _authenticationService.loginStatusChangeStream,
builder: (context, snapshot) {
if (snapshot.data == true) {
return Scaffold(
key: locator<DialogService>().scaffoldKey,
resizeToAvoidBottomInset: true,
drawer: DrawerView(),
appBar: AppBarCustom(),
body: _initializeViewPort(),
bottomNavigationBar: BottomNavigationView(),
);
}
return Scaffold(
resizeToAvoidBottomInset: true,
body: _initializeViewPort(),
bottomNavigationBar: BottomNavigationView(),
);
}),
));
I get The named parameter home isn't defined and The named parameter stream isn't defined, etc. etc.
This is the same for other fields too like:
Text(
title,
style: AppTheme.instance
.textStyleSmall(color: Colors.black, fontWeight: FontWeight.w600),
),
It tells me that style is not defined. I upgraded the project to current Flutter and am trying to remediate all the errors.
Can anyone help me out at all? It seems like Material just isn't installed. But I don't get an error when importing it at the top of the page?
Try running
flutter clean flutter pub get
if you are using android studio do file -> Invalidate cache and restart (find this at top bar)
make sure to import 'package:flutter/material.dart'; at top of your file
I encountered this problem a few days ago and I was not sure how, but I managed to solve it. I still don't have a clue about what actually causes it. Whenever I create a new flutter project and try to add asset images in my app it throws this error:
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/image01
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:672:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "assets/image01")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#abc8a(), name: "assets/image01", scale: 1.0)
====================================================================================================
I surfed for a while to find a solution but I was unable to find one, sometimes I just restart the app and run '''flutter clean''' to fix it but it isn't working this time. I don't know what to do, the pubsec.yaml and and the code seems to be working fine. I've tried creating new projects and rebuilding gradle but it's no good. Please provide descriptive and helpful answers.
Here's the code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.blue,
child: Image.asset('assets/image01'),
),
),
],
),
),
),
);
}
}
And here's pubsec.yaml
name: mi_card
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/
Try below code
Add below code in pubspec.yaml file.
flutter:
assets:
- assets/
uses-material-design: true
and create Image Widget, add your image path that save you in any folder like assets/image.png with your image extension
Center(
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.blue,
child: Image.asset('assets/image01.png'),
),
),
You have to specify the extension of your image (i.e. image01.png)
child: Image.asset('assets/image01.png')
First create folder - best name (images). Put your picture, all of the pictures you want, calling. Then go to the pubspec.yml page edit portion of the picture.
flutter:
uses-material-design: true //two space
assets: //same up space
- images/ //two speace after asset
Then go to your code add all paths. Example:
backgroundImage: AssetImage("images/ss.jpeg"),
Here's my first flutter app... Just learning and messing around a bit. I'm trying to figure out how to change the text of the title within the AppBar. Note that the text in the body changes, but the text in the app bar does not. Am I totally wrong here? I used this method based on other public questions on stack overflow on this topic. No luck on my end.
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App', style: TextStyle(fontFamily: 'Audowide'),),
centerTitle: true,
backgroundColor: Colors.cyan[700],
brightness: Brightness.dark,
),
body: Center(
child: Text("Body Text!",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
fontFamily: 'Audiowide',
letterSpacing: 2.0,
color: Colors.grey[500],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {}, // for later use
backgroundColor: Colors.cyan[700],
child: Text("Press"),
),
),
));
Step 1: Fix typo in your code change Audowide to Audiowide
Step 2: Download https://fonts.google.com/specimen/Audiowide
Step 3: Put file in directory assets/fonts
Step 4: In pubspec.yaml
fonts:
- family: Audiowide
fonts:
- asset: assets/fonts/Audiowide-Regular.ttf
working demo
You need to download the font, and put your reference in pubspec.yaml.
Look at how the documentation reports:
https://flutter.dev/docs/cookbook/design/fonts
You can try one of the 2 things:
Adding fonts manually to the project and defining them in pubspec.yaml, before using them in the project.
Another interesting package is google_fonts which is simpler and provides options for a lot many fonts that you might wanna try. The entire procedure to add it to your project and subsequent use is here
You can add the Google Font in your pubspec.yaml file.
dependencies:
google_fonts: ^1.1.1
Import the dependency in the Pubspec.yaml file
import 'package:google_fonts/google_fonts.dart';
I'm new in flutter, just started since last week. I'm learning from the online lesson, then i want to load a image in my flutter app. But there was an error which is :
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/image2.jpg
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:670:14)
Image provider: AssetImage(bundle: null, name: "assets/image2.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9f84b(), name: "assets/image2.jpg", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
Below is the main.dart 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(
"Welcome to HLH",
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
fontFamily: "Goldman",
),
),
centerTitle: false,
backgroundColor: Colors.amber,
),
body: Center(
child: Image(
image : AssetImage("assets/image2.jpg"),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Text("Login"),
),
);
}
}
Can anyone exists me how to solve it? I'm wondering is it the image problem? Thanks^^
This can be a error of not declaring the assets in your pubspec.yaml file.
flutter:
assets:
- images/
Add this to your pubspec.yaml file and then flutter pub get on the app root folder in terminal
I think this problem was caused due to the image path!
Make sure that the path of the image correctly in pubspec.yaml (must be the same with the image path directory).
like this:-
i saved my image in this path "assets/images/cat.png"
Pubspec.yaml file :
flutter:
assets:
- assets/images/
The images for the code below are not showing and it's throwing an exception. My pubspec is aligned correctly so not sure what's wrong.
I tried updating flutter, restarting,re-aligning but still get the same exception below.
Syncing files to device iPhone X... flutter: ══╡ EXCEPTION CAUGHT BY
IMAGE RESOURCE SERVICE
╞════════════════════════════════════════════════════ flutter: The
following assertion was thrown resolving an image codec: flutter:
Unable to load asset: /images/barlogo.png flutter: flutter: When the
exception was thrown, this was the stack: flutter: #0
PlatformAssetBundle.load
(package:flutter/src/services/asset_bundle.dart:221:7) flutter:
import 'package:flutter/material.dart';
void main() {
return runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: new Color(0xffA62FF0),
centerTitle: true,
elevation: 1.0,
leading: new Icon(Icons.camera_alt),
title: SizedBox(
height: 35.0, child:new Image.asset('/images/barlogo.png')),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: new Image.asset('/images/search.png'),
)
],
),
)
)
);
}
The exception gives it away - Flutter cannot find the image requested.
You should not have a leading / when specifying asset paths. Try:
Image.asset('images/barlogo.png')
There's more info in the documentation: https://api.flutter.dev/flutter/widgets/Image/Image.asset.html