You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Expo SDK 52、React Native 0.76环境下调用AsyncStorage.getItem报“AsyncStorage is null”错误求助

Expo SDK 52、React Native 0.76环境下调用AsyncStorage.getItem报“AsyncStorage is null”错误求助

嘿,我之前在Expo SDK 52的环境里也踩过类似的坑,给你几个亲测有效的排查和解决方向:

  • 确认导入方式是否正确
    从React Native 0.7x版本开始,官方已经移除了内置的AsyncStorage,必须使用@react-native-async-storage/async-storage这个独立包。你可以检查下自己的导入代码:
    正确的导入应该是:

    import AsyncStorage from '@react-native-async-storage/async-storage';
    

    如果之前用的是import { AsyncStorage } from 'react-native';,赶紧换掉,这大概率是导致“AsyncStorage is null”的核心原因。

  • 用Expo官方命令重新安装依赖
    虽然你已经装了AsyncStorage 1.23.1,但要确保安装的版本和Expo SDK 52完全兼容。建议用Expo专属命令重新安装:

    npx expo install @react-native-async-storage/async-storage
    

    这个命令会自动匹配适配你当前SDK版本的包,避免手动安装带来的版本不兼容问题。

  • 清除缓存并重启项目
    缓存堆积经常会引发奇怪的初始化问题,试试这几步:

    • 完全关闭Expo Go和本地开发服务器
    • 执行命令清除项目缓存:npx expo start --clear
    • 重新打开Expo Go扫码加载项目,再测试AsyncStorage功能
  • 检查Expo Go版本是否匹配
    Expo SDK 52需要对应版本的Expo Go支持,如果你的Expo Go是旧版本,很可能出现兼容性问题。去应用商店把Expo Go更新到最新版本后再测试。

  • 用极简代码排查干扰
    写一段无其他业务逻辑的测试代码,排除原有代码的干扰:

    import React, { useEffect } from 'react';
    import AsyncStorage from '@react-native-async-storage/async-storage';
    import { View, Text } from 'react-native';
    
    export default function TestAsyncStorage() {
      useEffect(() => {
        const testStorage = async () => {
          try {
            await AsyncStorage.setItem('test_key', 'test_value');
            const value = await AsyncStorage.getItem('test_key');
            console.log('存储的值:', value);
          } catch (err) {
            console.error('错误信息:', err);
          }
        };
        testStorage();
      }, []);
    
      return <View><Text>测试AsyncStorage</Text></View>;
    }
    

    如果这段代码能正常运行,说明问题出在你原有项目的其他逻辑里;如果还是报错,那就要再检查环境配置了。

希望这些方法能帮你解决问题!

内容来源于stack exchange

火山引擎 最新活动