【问题标题】:I can't get image in flutter我无法在颤动中获得图像
【发布时间】:2019-10-21 22:18:32
【问题描述】:

我无法从 assets 文件夹中获取图像。异常显示图像提供者:AssetImage(bundle: null, name: "assets/1.jpg") 请帮帮我

/flutter ( 7667): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 7667): The following assertion was thrown resolving an image codec:
I/flutter ( 7667): Unable to load asset: assets/1.jpg
I/flutter ( 7667): When the exception was thrown, this was the stack:
I/flutter ( 7667): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 7667): <asynchronous suspension>
I/flutter ( 7667): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:464:44)
I/flutter ( 7667): <asynchronous suspension>
I/flutter ( 7667): #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:449:14)
I/flutter ( 7667): #3      ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:315:48)
I/flutter ( 7667): #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:157:22)
I/flutter ( 7667): #5      ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:315:25)
I/flutter ( 7667): (elided 13 frames from package dart:async)
I/flutter ( 7667): Image provider: AssetImage(bundle: null, name: "assets/1.jpg")
I/flutter ( 7667): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#88414(), name: "assets/1.jpg", scale:
I/flutter ( 7667): 1.0)
I/flutter ( 7667): ════════════════════════════════════════════════════════════════════════════════════════════════════

【问题讨论】:

标签: flutter dart


【解决方案1】:

你需要把你的图片添加到pubspec.yaml:

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - images

我为此创建了一个 GitHub repo,请使用相同的文件结构,应该可以!

【讨论】:

  • 显示你的文件夹结构!
  • # 以下部分是针对 Flutter 的。 flutter: # 以下行确保 Material Icons 字体被包含在您的应用程序中,以便您可以使用 #material Icons 类中的图标。 uses-material-design: true # 要将资产添加到您的应用程序,请添加一个资产部分,如下所示: assets: - assets/1.jpg
  • 我为此创建了一个 GitHub 存储库,请使用相同的文件结构,应该可以! github.com/Taym95/stackoverflow_respons
  • 谢谢,现在可以了。因为我复制了你的文件 pubspec.yaml 。 # 材质图标类。 uses-material-design: true # 要将资产添加到您的应用程序,请添加一个资产部分,如下所示: assets: - assets/luchadortocat.1.png 只是,突然就可以了。我不知道它为什么起作用
【解决方案2】:

您必须在 pubspect.yaml 中提及用于项目的图片,如下所示。

flutter:
  assets:
  - assets/1.jpg

那么你可以像这样使用图像,

 Image.asset('images/1.jpg')

注意:-确保您的图片在名为“images”的文件夹中

【讨论】:

  • 我使用 child: GridTile(child: Image.asset('assets/1.jpg'), ),在我的代码中
  • 然后使用这个孩子:Image.asset('images/1.jpg'),
【解决方案3】:
import 'package:flutter/material.dart';
import 'package:kwanjai_next/constants/kwanjai_color.dart';

void main() => runApp(MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'GridView',
  home: ProjectTest(),
)); // MaterilApp

class ProjectTest extends StatefulWidget{
   @override
  ProjectTestState createState() => new ProjectTestState();
}

class ProjectTestState extends State<ProjectTest>{
 @override
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        backgroundColor: KwanjaiColors.greenst,       
        title: Text('Project t'),
      ),
      body: Container(
        child: ProjectList(),
      ),

    );
  }
}

class ProjectList extends StatefulWidget{
 @override
  ProjectListState createState() => new ProjectListState();
}

class ProjectListState extends State<ProjectList>{
  final list_item = [
    {
      "name" : "image 1",
      "picture" : "assets/1.jpg",
      "price" : 70,
      "old_picture" : 90
    },
        {
      "name" : "image 2",
      "picture" : "assets/2.jpg",
      "price" : 40,
      "old_picture" : 30
    },
        {
      "name" : "image 3",
      "picture" : "assets/3.jpg",
      "price" : 30,
      "old_picture" : 100
    },
        {
      "name" : "image 4",
      "picture" : "assets/4.jpg",
      "price" : 50,
      "old_picture" : 90
    },
        {
      "name" : "image 5",
      "picture" : "assets/5.jpg",
      "price" : 40,
      "old_picture" : 80
    },
        {
      "name" : "image 6",
      "picture" : "assets/6.jpg",
      "price" : 10,
      "old_picture" : 20
    },
        {
      "name" : "image 7",
      "picture" : "assets/1.jpg",
      "price" : 70,
      "old_picture" : 90
    },
        {
      "name" : "image 8",
      "picture" : "assets/2.jpg",
      "price" : 40,
      "old_picture" : 30
    },
        {
      "name" : "image 9",
      "picture" : "assets/3.jpg",
      "price" : 30,
      "old_picture" : 100
    },
        {
      "name" : "image 10",
      "picture" : "assets/4.jpg",
      "price" : 50,
      "old_picture" : 90
    },
        {
      "name" : "image 11",
      "picture" : "assets/5.jpg",
      "price" : 40,
      "old_picture" : 80
    },
        {
      "name" : "image 12",
      "picture" : "assets/6.jpg",
      "price" : 10,
      "old_picture" : 20
    },
  ]; 
   @override
  Widget build(BuildContext context){
    return GridView.builder( 

      itemCount: list_item.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), 

      itemBuilder: (BuildContext context, int index){
        return ProjectDetail(
          project_name: list_item[index]['name'],
          project_picture: list_item[index]['picture'],
          project_price: list_item[index]['price'],
          project_old: list_item[index]['old_picture'],
        );
      }
    );
  }
}

class ProjectDetail extends StatelessWidget{
  final project_name;
  final project_picture;
  final project_price;
  final project_old;

  ProjectDetail({this.project_name,this.project_picture,this.project_price,this.project_old});
  @override
  Widget build(BuildContext context){
    return Card(
      child: Hero(tag: project_name,
       child: Material(
         child: InkWell(
           onTap: (){},
           child:  GridTile(
             child:  Image.asset('assets/1.jpg'),

           ),
         ),
       ),),
    );
  }
}

【讨论】:

    【解决方案4】:

    在项目的主目录中创建名为 assets 的文件夹,并在创建文件夹中名为 images。在图像文件夹中添加名为 1.jpg 的图像。 在您的 pubspec.yaml 中:

    # The following section is specific to Flutter.
    flutter:
      # The following line ensures that the Material Icons font is
      # included with your application, so that you can use the icons in
      # the material Icons class.
      uses-material-design: true
    
      # To add assets to your application, add an assets section, like this:
      assets:
        - assets/images/1.jpg
    

    或添加整个目录的图像:

    # The following section is specific to Flutter.
    flutter:
    
      # The following line ensures that the Material Icons font is
      # included with your application, so that you can use the icons in
      # the material Icons class.
      uses-material-design: true
    
      # To add assets to your application, add an assets section, like this:
      assets:
        - assets/images/
    

    在您的代码中:

     Image.asset('assets/images/1.jpg')
    

    【讨论】:

      【解决方案5】:

      这对我有用 只需卸载然后重新安装应用程序即可清除设备中应用程序路径中的空文件或损坏文件。

      【讨论】:

        猜你喜欢
        • 2021-11-21
        • 2021-07-10
        • 1970-01-01
        • 2020-10-24
        • 1970-01-01
        • 2020-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        相关资源
        最近更新 更多