Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过fetchAPI获取数据后,FlatList在ios上呈现,而不是在android上_Android_Ios_React Native - Fatal编程技术网

通过fetchAPI获取数据后,FlatList在ios上呈现,而不是在android上

通过fetchAPI获取数据后,FlatList在ios上呈现,而不是在android上,android,ios,react-native,Android,Ios,React Native,我正在使用react native,并尝试根据使用fetch API获取的数据呈现一个简单的平面列表 下面是代码- class CategoryList extends React.Component { constructor(props) { super(props); var context = this; this.state = { categories: [] } }

我正在使用react native,并尝试根据使用fetch API获取的数据呈现一个简单的平面列表

下面是代码-

class CategoryList extends React.Component 
{
    constructor(props) 
    {
        super(props);
        var context = this;

        this.state = {
            categories: []
        }
    }

    componentDidMount()
    {
        fetch(api_config.url + api_config.routes.all_top_level_categories)
        .then(response => response.json())
        .then(data => this.setState({ categories: data}))
        .catch(e => console.log(e));
    }

    renderFlatListItem(data)
    {
        return(<View style={styles.item}>
            <Text>{data.item.name}</Text>
          </View>);
    }

    renderItemSeparator()
    {
        return(
            <View
                contentContainerStyle={styles.itemSeparator} />
        );
    }

    render() {
        return (
                <FlatList contentContainerStyle={styles.list}
                    data={this.state.categories}
                    renderItem={(data) => this.renderFlatListItem(data)}
                    ItemSeparatorComponent={this.renderItemSeparator}
                    keyExtractor={data => data.name}
                />
        );
    }
  }

export default class HomeScreen extends React.Component
{
    static navigationOptions = ({ navigation }) => {
      return {
        title: 'Home',
      }
    };
    render() {
      return (
        <CategoryList />
      );
    }  
}
class CategoryList扩展了React.Component
{
建造师(道具)
{
超级(道具);
var context=this;
此.state={
类别:[]
}
}
componentDidMount()
{
获取(api_config.url+api_config.routes.all_top_level_categories)
.then(response=>response.json())
.then(data=>this.setState({categories:data}))
.catch(e=>console.log(e));
}
renderFlatListItem(数据)
{
返回(
{data.item.name}
);
}
renderitemsparator()
{
返回(
);
}
render(){
返回(
this.renderFlatListItem(数据)}
ItemSeparatorComponent={this.renderItemSeparator}
keyExtractor={data=>data.name}
/>
);
}
}
导出默认类主屏幕扩展React.Component
{
静态导航选项=({navigation})=>{
返回{
标题:"家",,
}
};
render(){
返回(
);
}  
}
下面是Android的图片-

下面是iOS的图片-


在此问题上的任何帮助都将不胜感激。谢谢。

你能试试这个组件吗?{setTimeout(()=>{this.fetchfunction();},400)}fetchfunction(){fetch(api_config.url+api_config.routes.all_top_level_categories)。然后(response=>response.json())。然后(data=>this.setState({categories:data})。catch(e=>console.log(e));}@Pramod感谢您的回复,但它不起作用,也尝试将超时时间增加到1000。这种方法有什么特殊原因吗?获取数据需要时间。因此,在呈现视图时。请在“ComponentWillMount”中尝试此获取组件功能。我会尽量让你know@Pramod这是因为android emulator的ip地址指向本地主机,而不是主机的ip,即android的10.0.2.2。我犯了一个愚蠢的错误。