【问题标题】:Value of uri cannot be cast from double to stringuri 的值不能从 double 转换为 string
【发布时间】:2022-01-06 08:53:48
【问题描述】:

我正在使用 Yelp Api,我尝试搜索餐厅,但我遇到了餐厅图片问题 我没有问题地获得名称和评级,但图像是第一次显示,但是当我重新加载我的应用程序时出现此错误:uri 的值不能从双精度转换为字符串

代码如下:

const RestaurantItems=(props)=>{
    return(
        
        <ScrollView>
    <View style={props.theme=='light'?styles.container:styles.darkContainer}>
        
        {props.restaurantsData.map((item,index)=>(
        
            <View style={{marginTop:20}} key={index}>
                 <View style={styles.Img}>
              <Image source={{uri:item.image_url}}style={{width:'100%',height:180}} />
              

        </View>
        <View style={{paddingLeft:10,marginTop:15,flexDirection:"row",justifyContent:'space-between',alignItems:"center"}}>
            <View>
              <Text style={[{fontSize:19,fontWeight:"700"},props.theme=='dark'?{color:'white'}:{color:'black'}]}>{item.name}</Text>
              <Text style={{color:'#717171'}}>35-45 m</Text>
              </View>
              <View style={{marginRight:10,backgroundColor:"#eee",paddingHorizontal:7,borderRadius:30}}>
                  <Text style={{fontSize:19,fontWeight:"500"}}>{item.rating}</Text>
              </View>
        </View>
            </View>
        ))}
        </View>
        </ScrollView>
       

       
    )}```

【问题讨论】:

    标签: react-native api yelp-fusion-api


    【解决方案1】:

    item.image 应该是 url 字符串检查 props.restaurantsData 的值可能是索引之一,将 image_url 作为数字或尝试将 item.image 替换为 item.image_url.toString()

    【讨论】:

    • props.restaurantsData 是从 yelp API 中提取的,我如何查看我的 api 结果以便检查?我尝试了 console.log 但它不起作用!我不明白为什么它在第一次工作但如果我刷新它会给我错误
    • 可能是第一次渲染后道具正在更新。要查看数据,您可以执行上面的 console.log(props.restaurantsData) 返回或在屏幕上查看&lt;Text&gt;{JSON.stringify(props.restaurantsData)}&lt;/Text&gt;
    • 控制台日志现在可以工作了,我可以看到,我尝试了 toString() 函数,它现在工作了,即使在我重新加载后它也能显示图像!非常感谢你节省了很多时间,如果这不打扰你,我想问你另一个问题,当我控制台记录数据时,image_url 已经是一个字符串,我必须让它再次成为字符串才能工作的逻辑是什么? ?
    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2020-04-05
    • 2013-05-02
    相关资源
    最近更新 更多