【问题标题】:JSON value '<null>' of type NSNull cannot be converted to NSStringNSNull 类型的 JSON 值“<null>”无法转换为 NSString
【发布时间】:2019-05-02 04:21:37
【问题描述】:

当我在 iOS 模拟器中重新加载我的 React Native 应用程序时,有时会收到以下错误屏幕。

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

我在我的应用中使用 SignalR 客户端以及 Firebase 消息传递。

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

非常令人沮丧。

【问题讨论】:

    标签: ios react-native simulator


    【解决方案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});
                 };
            });
    

    【讨论】:

    • 此错误与我的代码中的 AsyncStorage 无关。它只会在我进行 GraphQL 调用的任何项目中随机发生。我没有为 graphQL 使用任何第三方库,所以它也与 Apolo 无关。
    • 对。我是说把 if(value){} 放在那里可能会有所帮助。
    【解决方案2】:

    我的问题在于组件中的图像来源。我告诉图像组件将 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}/>
    

    【讨论】:

    • 感谢您的提示。这是一个常见的原因。拯救了我的一天。
    【解决方案3】:

    不知道 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
    

    我希望这将有助于任何人在未来寻找解决方案。

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        您的问题很可能是由于某些地方未在您的本地 IOS 代码中定义的。

        当我忘记在 AppDelegate.m 中导入 React 本机 IOS 通知所需的包时收到此错误

        【讨论】:

          猜你喜欢
          • 2017-04-10
          • 1970-01-01
          • 1970-01-01
          • 2021-01-14
          • 2016-06-06
          • 2016-11-17
          • 2016-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多