NSNull类型的JSON值'<null>'无法转换为NSString

时间:2018-11-30 02:16:43

标签: ios react-native simulator

当我在iOS Simulator中重新加载React Native应用程序时,有时会出现以下错误屏幕。

enter image description here

有时根本不会发生,但有时一天中会发生几次。

我正在我的应用程序以及Firebase Messaging中使用SignalR客户端。

我已尝试在所有应用程序中放入 try catch 尝试,包括使用AsyncStorage的应用程序,但这种情况仍在发生。

非常令人沮丧。

4 个答案:

答案 0 :(得分:8)

我的问题是组件中图像的来源。我已经告诉图像组件将null作为源传递,而服务器对图像的响应为空或null。意思是:

<Image source={{uri: null}} style={styles.myImageStyle}/>

对于android来说还可以,但是在ios中我遇到了此错误。所以我将代码更改为关注代码,并且现在可以正常工作。

const imageUri = myImageUri!=null ? myImageUri : ""

<Image source={imageUri.length!=0?{uri: imageuri}: null} style={styles.myImageStyle}/>

答案 1 :(得分:1)

我在使用AsyncStorage时遇到了这个问题。它只能存储字符串,因此无法存储其他任何东西,例如对象等。下面是我设置和获取一系列被阻止用户的方法。

设置项目时,将其字符串化:

AsyncStorage.setItem('blocked_users', JSON.stringify(array));

然后,当您从异步存储中读取数据时,请使用promise和json解析数据:

AsyncStorage.getItem("blocked_users")
        .then((value) => {
             if(value){
               blocksArray = JSON.parse(value);
               this.setState({ blocked_users: blocksArray});
             };
        });

答案 2 :(得分:0)

不知道OP是否有解决方案,但我一直在与这个错误作斗争,并且能够提出解决方案。该错误源自AsyncStorage。我要解决的问题是使用条件语句并将默认值设置为Null

  componentDidMount() {

  AsyncStorage.getItem('Key_0').then((value) => 

this.setState({getValue: value}))
}...
...var avatar 
if(this.state.getValue == null){
avatar = require('path/to/palceholder/asset);
}else if(this.state.getValue ! = null){
avatar={uri: this.state.getValue};
} 

<Image source={avatar}/> //the image source was the root cause of the error since Asyncstorage was passing null value

我希望这将对以后寻求解决方案的所有人有所帮助。

答案 3 :(得分:0)

我的问题是我在组件中拥有的个人资料图像的来源。

source={profileImage && profileImage ? {uri: profileImage} : null} 

检查个人资料图像是否存在,然后添加条件。