I am currently learning Flutter for Web and have encountered an error when trying to load an image asset. Despite following the typical asset loading procedures, I’m facing an “Unable to load asset” error. Here’s a detailed breakdown of my issue, including the relevant code snippets and file structure:
lib/main.dart
import 'package:dogs/profile_screen.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ProfileScreen(),
);
}
}
lib/profile_screen.dart
import 'package:flutter/material.dart';
class ProfileScreen extends StatelessWidget {
const ProfileScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: const [
ProfileImage(),
ProfileDetails(),
ProfileActions(),
],
),
);
}
}
class ProfileImage extends StatelessWidget {
const ProfileImage({super.key});
@override
Widget build(BuildContext context) {
return ClipOval(
child: Image.asset(
width: 200,
height: 200,
'assets/dog1.jpg',
fit: BoxFit.fitWidth,
),
);
}
}
class ProfileDetails extends StatelessWidget {
const ProfileDetails({super.key});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Wolfram Barkovich',
style: TextStyle(fontSize: 35, fontWeight: FontWeight.w600),
),
_buildDetailsRow('Age', '4'),
_buildDetailsRow('Status', 'Good Boy'),
],
),
);
}
Widget _buildDetailsRow(String heading, String value) {
return Row(
children: [
Text(
'$heading: ',
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(value),
],
);
}
}
class ProfileActions extends StatelessWidget {
const ProfileActions({super.key});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildIcon(Icons.restaurant, 'Feed'),
_buildIcon(Icons.favorite, 'Pet'),
_buildIcon(Icons.directions_walk, 'Walk'),
],
);
}
Widget _buildIcon(IconData icon, String text) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Icon(icon, size: 40),
Text(text),
],
),
);
}
}
pubspec.yaml
assets:
- assets/
$ ls -1 assets/
dog1.jpg
khachik-simonian-nXOB-wh4Oyc-unsplash.jpg