Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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
Android null不是一个对象_Android_React Native_Asyncstorage - Fatal编程技术网

Android null不是一个对象

Android null不是一个对象,android,react-native,asyncstorage,Android,React Native,Asyncstorage,我正在尝试从异步存储中获取存储值。但它给出的null不是对象错误。 这是我的密码 import React, { Component } from 'react'; import { AsyncStorage, Image, View, StatusBar } from 'react-native'; import { connect } from 'react-redux'; import { Container, Button, H3, Text, Header, Title, Conten

我正在尝试从异步存储中获取存储值。但它给出的null不是对象错误。 这是我的密码

import React, { Component } from 'react';
import { AsyncStorage, Image, View, StatusBar } from 'react-native';
import { connect } from 'react-redux';
import { Container, Button, H3, Text, Header, Title, Content, Icon, Footer, FooterTab, Left, Right, Body } from 'native-base';

import { openDrawer } from '../../actions/drawer';
import styles from './styles';
import Anatomy from '../anatomy/index.js';

const launchscreenBg = require('../../../img/abc.png');
const launchscreenLogo = require('../../../img/mob_app_bg.png');

var STORAGE_KEY_USER = '@ExpLocal:user';

class Home extends Component { // eslint-disable-line

  componentDidMount() {
    this._loadInitialState().done();
  }

  async _loadInitialState() {
    try {
      var user = await AsyncStorage.getItem(STORAGE_KEY_USER);
      if (user !== null){
        this.setState({selectedUser: user});
        this._appendMessage('Logged In User: ' + user);
      } else {
        this._appendMessage('Empty');
      }
    } catch (error) {
      this._appendMessage('AsyncStorage error: ' + error.message);
    }
  }

  getInitialState() {
    return {
      selectedUser: null,
      messages: [],
    };
  }

  static propTypes = {
    openDrawer: React.PropTypes.func,
  }

  render() {
    this._loadInitialState().done();
    // var loggedUser = this.state.selectedUser;
    if(this.state.selectedUser !== null){
      return (
        <Container style={styles.container}>
        <Header>
        <Left>
        <Button transparent onPress={this.props.openDrawer}>
        <Icon name="ios-menu" />
        </Button>
        </Left>
        <Body>
        <Title>Home</Title>
        </Body>
        <Right />
        </Header>
        </Container>
        );
    }else{
      return (
        <Anatomy />
        );
    }

  }
}

function bindActions(dispatch) {
  return {
    openDrawer: () => dispatch(openDrawer()),
  };
}

const mapStateToProps = state => ({
  navigation: state.cardNavigation,
  themeState: state.drawer.themeState,
  routes: state.drawer.routes,
});

export default connect(mapStateToProps, bindActions)(Home);
import React,{Component}来自'React';
从“react native”导入{AsyncStorage,Image,View,StatusBar};
从'react redux'导入{connect};
从“本机基础”导入{容器、按钮、H3、文本、页眉、标题、内容、图标、页脚、页脚选项卡、左、右、正文};
从“../../actions/drawer”导入{openDrawer};
从“./styles”导入样式;
从“../analysis/index.js”导入解剖结构;
const launchscreenBg=require('../../../img/abc.png');
const launchscreenLogo=require('../../../img/mob_app_bg.png');
var STORAGE_KEY_USER='@ExpLocal:USER';
类Home扩展组件{//eslint禁用行
componentDidMount(){
这是。_loadInitialState().done();
}
异步_loadInitialState(){
试一试{
var user=await AsyncStorage.getItem(存储\密钥\用户);
如果(用户!==null){
this.setState({selectedUser:user});
此._追加消息('登录用户:'+用户);
}否则{
此._appendMessage('Empty');
}
}捕获(错误){
此._appendMessage('AsyncStorage错误:'+error.message);
}
}
getInitialState(){
返回{
selectedUser:null,
信息:[],
};
}
静态类型={
openDrawer:React.PropTypes.func,
}
render(){
这是。_loadInitialState().done();
//var loggedUser=this.state.selectedUser;
if(this.state.selectedUser!==null){
返回(
家
);
}否则{
返回(
);
}
}
}
职能行动(调度){
返回{
openDrawer:()=>分派(openDrawer()),
};
}
常量mapStateToProps=状态=>({
导航:state.cardNavigation,
themeState:state.drawer.themeState,
路线:state.drawer.routes,
});
导出默认连接(MapStateTops、bindActions)(主);
给出以下错误 null不是对象(计算'this.state.selectedUser') E:\REACT\u NATIVE\u PROJECTS\NativeBase KitchenSink master\js\components\home\index.js:49:18
我做错了什么?

您可以这样使用:

AsyncStorage.getItem("STORAGE_KEY_USER").then((value) => {
   var user = value;
  //Your functionalities
}).catch((error) => {
  //Catch Block
});

您只需检查变量是否具有实际值。换句话说

if( value ) {}

如果有值,则得到true,如果没有值,则得到false

你的意思是这样的吗?async\u loadInitialState(){AsyncStorage.getItem(“STORAGE\u KEY\u USER”)。然后((value)=>{var USER=value;this.setStatus({selectedUser:USER})//Your functionaties})。catch((错误)=>{//catch Block})}是的@NeshanManilka