【问题标题】:React-Native-AsyncStorage save integer itemReact-Native-AsyncStorage 保存整数项
【发布时间】:2019-08-02 06:44:13
【问题描述】:

我尝试将整数变量保存到 AsyncStorage,但它返回错误

索引 1 处的绑定值为 null

设置项目

这是我保存项目的代码

AsyncStorage.setItem(
      Common.CURRENT_CHILD,
      this.state.child.toString(), //14
      () => {
        NavigationService.navigate('Home', {
          childName: this.state.childName
        });
      }
    );

获取项目

这是我检索项目的代码

    AsyncStorage.getItem(Common.CURRENT_CHILD, (err, result) => {
      if (result === null) {
       console.log(err);
     } else {
      this.setState(
     {
        child: result
     },
      () => {
        console.log('Result -', result);
      }
    );
  }
})

【问题讨论】:

  • 您是否要在导航到主页后立即获取它?您还记录了this.state.child.toString() 值吗?
  • 我只是在保存项目后导航到主页。 this.state.child.toString 将是整数变量,例如14
  • 试试await AsyncStorage.setItem,看看效果如何。

标签: reactjs react-native asyncstorage


【解决方案1】:
AsyncStorage.setItem(
  Common.CURRENT_CHILD,
 ""+ this.state.child.toString(), //14
  () => {
    NavigationService.navigate('Home', {
      childName: this.state.childName
    });
  }
);

您可以在将项目设置为 Asyncstorage 时编写“”,并在 if 语句或之前获取项目时尝试使用 console.log

  AsyncStorage.setItem(
      Common.CURRENT_CHILD,
     ""+ this.state.child, //14
      () => {
        NavigationService.navigate('Home', {
          childName: this.state.childName
        });
      }
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多