> For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt.

# getStorageSync

从本地缓存同步读取数据。

## 扫码预览

![扫码预览 getStorageSync](https://api.qrserver.com/v1/create-qr-code/?size=200x200\&data=doubao%3A%2F%2Fdoubao_apps%3Fapp_id%3Ddb_1McM5Ni%26path%3D%252Fpages%252Fapi%252Fstorage%252Fset-storage%252Findex)

## 使用限制

> \[!WARNING]
> **Android、iOS**：key 不存在时 Android 返回 null、iOS 返回 nil，且均按成功返回，不抛出错误；调用方按必返字段使用会得到 undefined，且无法区分「未写入」与「写入了空值」。

## 支持版本

前端库版本不低于 `0.0.19`。

## 支持平台

<table>
<thead>
<tr><th>平台</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>Android</td><td>支持</td></tr>
<tr><td>iOS</td><td>支持</td></tr>
<tr><td>PC</td><td>不支持</td></tr>
<tr><td>HarmonyOS</td><td>不支持</td></tr>
</tbody>
</table>

## 支持场景

<table>
<thead>
<tr><th>场景</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>页面</td><td>支持</td></tr>
<tr><td>卡片</td><td>支持</td></tr>
</tbody>
</table>

## 使用说明

- 写入时会对 data 做 JSON 序列化，读取时按 JSON 反序列化，无法解析时原样返回字符串。

## 调用方式

### 同步 API

```typescript
getStorageSync<TData = unknown>(params: GetStorageParams): GetStorageResult<TData>
```

## 入参

<table>
<thead>
<tr><th>名称</th><th>类型</th><th>必填</th><th>默认值</th><th>约束</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td><code>key</code></td><td><code>string</code></td><td>是</td><td>-</td><td>-</td><td>本地缓存键名</td></tr>
</tbody>
</table>

## 调用示例

```typescript
import { getStorageSync } from '@doubao-dev/framework/api';

const result = getStorageSync<{ name: string; age: number }>({
  key: 'profile',
});

console.log(result.data.name, result.data.age);
```

## 返回值

<table>
<thead>
<tr><th>名称</th><th>类型</th><th>必返</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td><code>data</code></td><td><code>TData</code></td><td>是</td><td>key 对应的数据。Android、iOS：key 不存在时 Android 返回 null、iOS 返回 nil，且均按成功返回，不抛出错误。</td></tr>
</tbody>
</table>

### 返回示例

```json
{
  "data": {
    "name": "Tom",
    "age": 18
  }
}
```

## 错误处理

同步调用失败时抛出错误。错误对象和通用错误码见 [通用错误处理](/apps-plugin/docs/js-api/common-errors.md#错误返回)。

### 失败示例

```json
{
  "errNo": 1600004,
  "errMsg": "key is empty"
}
```

### 错误码

<table>
<thead>
<tr><th>errNo</th><th>errMsg</th><th>平台</th><th>触发条件</th><th>处理建议</th></tr>
</thead>
<tbody>
<tr><td><code>1600004</code></td><td><code>key is empty</code></td><td>Android、iOS</td><td>key 为空字符串</td><td>传入非空 key。</td></tr>
<tr><td><code>1600005</code></td><td><code>key exceeds max length</code></td><td>Android、iOS</td><td>key 长度超过 1024</td><td>缩短 key 长度。</td></tr>
</tbody>
</table>

#### 通用错误码

<table>
<thead>
<tr><th>errNo</th><th>errMsg</th><th>说明</th><th>平台</th></tr>
</thead>
<tbody>
<tr><td><code>102</code></td><td><code>internal error</code></td><td>内部错误</td><td>Android</td></tr>
</tbody>
</table>
