简体   繁体   English

Flatlist不会显示来自json的图像(React Native)

[英]Flatlist won't show images from json (React Native)

i've been having this issue with my images not showing in my application. 我一直遇到这个问题,我的图像未显示在我的应用程序中。

Im using FlatList: 我正在使用FlatList: colors.js

Here is my javascript file holding the json which im importing into the View: 这是我的javascript文件,其中包含即时消息导入视图的json: json档案

I've been trying so many ways to get the Image component to accepts the images but its just not showing, many of the ways i've been trying doesn't show as an error, the images is just not showing at all. 我一直在尝试许多方法来使Image组件接受图像,但它只是不显示,我一直在尝试的许多方法都不会显示为错误,而图像根本不会显示。

Hope someone can help me out of this dilemma, been stuck for days and googling as a mad man -.- 希望有人可以帮助我摆脱这个困境,被困了几天,并以一个疯子的身份谷歌搜索-.-

You have 2 problems in your code: 您的代码中有2个问题:

  1. you need to use require instead of { uri: '' } for local images 您需要对本地图像使用require而不是{ uri: '' }
  2. when you use require you can't pass a variable as path, so you need to pass just literal string. 当使用require您不能将变量作为路径传递,因此您只需要传递文字字符串。

So replace each path line in your colors array to include require 因此,替换颜色数组中的每条路径线以包括require

example: 例:

{
  id: 1,
  path: require('../../assets/character-customs/albert/colors/c1.png'),
}

Also update Image component line with this 与此同时更新Image组件行

<Image style={styles.images} source={item.path} />

This should work for you 这应该为你工作

For me flatlist was wrapping all data inside item so this fixed the problem for me 对我来说,flatlist将所有数据包装在项目中,因此这为我解决了问题

 <Image source={{uri:item.path}}/>

if this doesnt work try to log item from from renderitem method 如果这不起作用,请尝试从renderitem方法记录项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM