I'm getting an error running my app, because the app is unable to load the asset. I've tried everything, I checked the indentation and the spelling of the path.
Here there's the code:
Widget build(BuildContext context) {
return MaterialApp(
theme: MyAppTheme.lightTheme,
darkTheme: MyAppTheme.darkTheme,
themeMode: ThemeMode.system,
home: SplashScreen(),
);
}
Inside the main.dart I invoke the SplashScreen:
Widget build(BuildContext context) {
return const Scaffold(
body: Stack(
children: [
Positioned(
child: Image(
image: AssetImage(iconaCerchioBlu)
)
)
],
),
);
}
That screen should show me the image while running the app. "iconaCerchioBlu" is defined in a separate class like that:
const String iconaCerchioBlu = "/assets/images/cerchioBlu.png";
const String iconaCerchioVerde = "/assets/images/cerchioVerde.png";
I've imported the class into the SplashScreen class. My project Folders are like this:
N.B. I've also tried to Add this code in pubspec.yaml (just in case):
flutter:
uses-material-design: true
assets:
- assets/images/
Obv, it didn't work.
I've read the other questions here in the forum and tried the solution proposed but it didn't work in this case. Any idea?
Try these two options, sometimes it's tricky to add images in your Flutter App. Under the assets folder definition in your pubspec.yml file move the dash to be under the "e" like this: assets:
Also, try removing the first slash in the variable definition like this: onst String iconaCerchioBlu = "assets/images/cerchioBlu.png";
Try one of these at a time to see which solves your problem.