我使用Visual Studio代码和Andriod Studio IDE以及其他AVD管理器,但是当我使用“图片”资产运行应用程序时出现错误,没有找到资产的文件或变体:assets / puffin.jpeg。
简单的例子: 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
扑扑医生
[√] 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!
答案 0 :(得分:0)
首先,请确保资产文件夹位于项目的根目录中,与lib文件夹处于同一级别,如下所示:
my_project
-android
-assets //your image goes here, puffin.jpeg
-ios
-lib
-pubspec.yaml
-etc...
如果您的资产文件夹位于另一个文件夹中,则IDE不会在其中加载内容。另外,有时如果您不先保存,则vs代码不会将更改加载到pubspec文件中,因此,请确保在IDE终端中运行“ flutter pub get”之前先保存项目。
答案 1 :(得分:0)
我修复了一个错误,删除了文件夹 .dart_tool ,然后重新运行应用程序,错误消失了。