【问题标题】:Flutter get Image.Asset from ListFlutter 从 List 中获取 Image.Asset
【发布时间】:2022-01-10 22:07:23
【问题描述】:

所以我想从一个列表中获取图像,其中包含为项目提供的图像的文件名,如下所示。

这是我们的清单:

const product = [
  {
    "assets": "pizza.png",
    "name": "Mathematics Tasks",
    "info": "Quiz on Module 3",
    "score": 20,
  },
  {
    "assets": "burger.png",
    "name": "Science Tasks",
    "info": "Quiz on Module 8",
    "score": 50,
  },
  {
    "assets": "fries.png",
    "name": "Technology Tasks",
    "info": "Activity on Module 2",
    "score": 5,
  },
  {
    "assets": "hotdog.png",
    "name": "Arts Tasks",
    "info": "Quiz on Module 3",
    "score": 20,
  },
];

以及我们如何通过以下代码获取每个人:

Image.asset("assets/${product['assets']}")

如上图所示,它不会显示它,但我们确信代码可以正常工作。我们甚至在 pubspec.yaml 文件中声明了它的位置,指向 assets 文件夹中的图像。我知道我们可以分别插入文件的名称,但我们认为如果我们将其保存在一个列表中以避免使事情过于复杂,它会更容易和更有条理。是如何完成的有问题还是变量是问题所在?

对于那些要求 pubspec.yaml 文件的人,这里是:

name: test_project
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2 

dev_dependencies:
  flutter_test:
    sdk: flutter

  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^1.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# 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/

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

不,在这种情况下我不使用其他文件夹,但如果必须,我会添加“资产/文件夹”。不添加使用的包,因为这并不完全使用包...

【问题讨论】:

  • product 是从 api 获取的吗?您能否说明您是如何构建yaml 的?
  • 请同时提供 pubspec.yaml 文件。以便我们了解您如何添加图片。

标签: flutter image dart


【解决方案1】:

试试这个

在您的项目中,在资产文件夹中创建一个名为 assets 的文件夹,在其中创建另一个名为 png 的文件夹。

所以安排将是

assets
   |__ png

现在转到您的 pubspec.yaml 将路径添加到您的资产

assets:
    - assets/png/

现在您已完成添加它们,为此创建一个常量并将其命名为 image.dart,具体取决于您的名称。现在在 image.dart 中

const String? pizza = "assets/png/pizza.png";
const String? burger = "assets/png/burger.png";

类似的东西。

现在在您的列表中编辑那些喜欢这个并命名图像名称。

const product = [
  {
    "assets": pizza!,
    "name": "Mathematics Tasks",
    "info": "Quiz on Module 3",
    "score": 20,
  },
  {
    "assets": burger!,
    "name": "Science Tasks",
    "info": "Quiz on Module 8",
    "score": 50,
  }
];

现在最后当你打电话给他们时,它仍然像你一样打电话。

Image.asset(product[index]['assets'])

Image.asset(product['assets'])

我希望它可以帮助我通常这样做。

【讨论】:

  • 嗯,这确实有效,但是在那里有刺痛并从列表中调用单个索引可以在不需要多个字符串的情况下获得整个列表。虽然我认为我暂时可以使用它。
猜你喜欢
  • 1970-01-01
  • 2021-01-23
  • 2021-06-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多