0

我正在尝试从 API ( https://developers.zomato.com/documentation ) 检索数据并获取餐厅的标题和图像。但是,当我尝试加载图像时,会收到警告source.uri should not be an empty string

这是我的代码:

async componentDidMount() {
    let id = this.props.navigation.state.params.category
    let result;
    try {
      result = await axios.request({
        method: 'get',
        url: `https://developers.zomato.com/api/v2.1/search?category=${id}`,
        headers: {
          'Content-Type': 'application/json',
          'user-key': "a31bd76da32396a27b6906bf0ca707a2",
        },
      })
    } catch (err) {
      err => console.log(err)
    }
    this.setState({
      isLoading: false,
      data: result.data.restaurants
    })
  }
render() {
    return (
      <View>
        {
          this.state.isLoading ?
            <View style={{ flex: 1, padding: 20 }}>
              <ActivityIndicator style={{color:'red'}} />
            </View> :
            (
              this.state.data.length == 0 ?
                <View style={{ flex: 1, padding: 20 }}>
                  <Text style={{ color: '#000', fontWeight: 'bold' }}>No restaurants from selected category</Text>
                </View> :
                <FlatList
                  style={{ marginBottom: 80 }}
                  keyExtractor={item => item.id}
                  data={this.state.data}
                  renderItem={({ item }) =>
                  <TouchableHighlight onPress={()=> console.log(item.restaurant.thumb)}>
         <Card image={item.restaurant.thumb} style={styles.container}>
            <Image resizeMode='contain' source={{ uri: item.restaurant.thumb }}/>
           <Text style={{color:'#000',fontWeight:'bold'}}>{item.restaurant.name} </Text>
         </Card>
         </TouchableHighlight>
                  }
                />
            )
        }
      </View>
    );
  }

正如您所看到的,当我触摸任何一张卡片时,我正在控制台记录图像 uri 的链接,它完美地显示出来。为什么当应用程序加载图像时它们是空字符串,而当我通过控制台日志加载它时,链接完美显示?

我正在使用 axios 加载我的 API

这是一个世博小吃链接:https ://snack.expo.io/r1XTaw4JU

4

1 回答 1

1

所以我遇到了 2 个问题,一个是卡组件中您没有正确提供 uri,它应该是image={{uri:item.restaurant.thumb}},其次对于 newyork,您的实体 ID 必须是

要在 'Manhattan, New York City' 中搜索 'Italian' 餐厅,请设置美食 = 55、entity_id = 94741 和 entity_type = zone

它根据 zomato 文档,所以请检查一下。并找到博览会链接:expo-snack

import React from 'react';
import { 
    View,
    Text,
    FlatList,
    StyleSheet,
    Button,
    TouchableHighlight,
    ActivityIndicator,
    } from 'react-native';
import { createAppContainer } from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { Card, Image } from 'react-native-elements';
import Constants from 'expo-constants';
import axios from 'axios';

export default class CategoryScreen extends React.Component {

  constructor(props){
    super(props);
        this.state={
      data : [],
      isVisible: true,
      city : '94741'
    }
  }
async componentDidMount() {
    let id = "3"
    let city = this.state.city
    let result;
    try {
      result = await axios.request({
        method: 'get',
        url: `https://developers.zomato.com/api/v2.1/search?entity_id=${city}&entity_type=zone&category=${id}`,
        headers: {
          'Content-Type': 'application/json',
          'user-key': "a31bd76da32396a27b6906bf0ca707a2",
        },
      })
    } catch (err) {
      err => console.log(err)
    }
    this.setState({
      isLoading: false,
      data: result.data.restaurants
    })
    console.log(result)
    console.log(data)
  }
render() {
    return (
      <View>
        {
          this.state.isLoading ?
            <View style={{ flex: 1, padding: 20 }}>
              <ActivityIndicator style={{color:'red'}} />
            </View> :
            (
              this.state.data.length == 0 ?
                <View style={{ flex: 1, padding: 20 }}>
                  <Text style={{ color: '#000', fontWeight: 'bold' }}>No restaurants from selected category</Text>
                </View> :
                <FlatList
                  style={{ marginBottom: 80 }}
                  keyExtractor={item => item.id}
                  data={this.state.data}
                  renderItem={({ item }) =>
                  <TouchableHighlight onPress={()=> alert(item.restaurant.location.city)}>
         <Card image={{uri:item.restaurant.thumb}} style={styles.container}>
           <Text style={{color:'#000',fontWeight:'bold'}}>{item.restaurant.name} </Text>
         </Card>
         </TouchableHighlight>
                  }
                />
            )
        }
      </View>
    );
  }

};

const styles = StyleSheet.create({

});
于 2019-12-28T06:08:39.597 回答