问题
I use Visual studio code and Andriod studio IDE and different AVD managers but when I run app with Image asset get an error No file or variants found for asset: assets/puffin.jpeg.
Simple example: main.dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var title = 'Web Images';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Container(
alignment: Alignment.center,
child: Image.asset(
"assets/puffin.jpeg",
width: 280.0,
),
)
),
);
}
}
pubspec.yaml
name: flutter_app_test
description: A new Flutter application.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/puffin.jpeg
run flutter doctor
[√] Flutter (Channel stable, v1.17.5, on Microsoft Windows [Version 10.0.18363.959], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
[√] Android Studio (version 4.0)
[√] VS Code (version 1.47.3)
[√] Connected device (1 available)
• No issues found!
回答1:
First of all make sure that your assets folder is in the root of your project, at the same level of the lib folder, like this:
my_project
-android
-assets //your image goes here, puffin.jpeg
-ios
-lib
-pubspec.yaml
-etc...
If you have your assets folder inside of another folder the IDE will not load the content inside it. Also, sometimes vs code does not load the changes in the pubspec file if you does not save it first, so, make sure that you save your project before you run "flutter pub get" in the terminal of the IDE.
回答2:
I fix an error I delete folder .dart_tool and rerun app and error gone.
来源:https://stackoverflow.com/questions/63306627/flutter-assets-image-not-find