lollipopkit / lollipopkit/flutter_server_box

[Bug] 剪贴板导入代码片段:包含中文时报 FormatException: Unexpected extension byte

Open Beginner friendly
#1,513 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Dart
Stars
8.7k
Forks
558
Avg merge
4h 31m
Merged PRs (30d)
129

Description

## 描述 Bug

通过「剪贴板」方式导入包含中文(或任何非 ASCII 字符)的代码片段 JSON 时,报错 `FormatException: Unexpected extension byte (at offset N)`,导入失败。

## 复现步骤

1. 在任意编辑器(如 Zed/VS Code)中创建包含中文的代码片段 JSON(如 `{"name": "启动服务", ...}`),保存为 UTF-8
2. 复制整个 JSON 文本
3. ServerBox → 设置 → 备份/数据 → 导入 → 代码片段 → 选择「剪贴板」方式
4. 报错 `FormatException: Unexpected extension byte (at offset N)`,导入失败

## 期望结果

包含中文的合法 UTF-8 JSON 能正常导入。

## 实际结果

导入失败。已验证文件本身是合法 UTF-8(Zed 保存),非文件编码问题。

## 根因定位(已核实源码)

问题在剪贴板导入的字节转换,`fl_lib` 的 `showImportDialog` 剪贴板分支(`lib/src/core/ext/ctx/dialog.dart`):

```dart
case _ImportFrom.clipboard:
final text = await Pfs.paste();
if (text == null) return null;
return Uint8List.fromList(text.codeUnits); // ← 问题行
```

`text.codeUnits` 返回 **UTF-16 码元**(如"启"= 0x542F),`Uint8List.fromList` 强转时**截断为低 8 位**(0x2F),产生非法 UTF-8 字节序列。随后片段导入处 `lib/view/page/backup.dart:586` 的 `utf8.decode(data)`(严格模式)抛 `FormatException`。

## 建议修复

```dart
return Uint8List.fromList(utf8.encode(text)); // 标准 UTF-8 编码
```

一行改动。注意 `file`/`network` 分支走真实字节流,不受影响;仅 `clipboard` 分支有此问题。

## 设备

- 系统:Android 9 (SDK 28)
- 设备:vivo V1813BT (arm64-v8a)
- App 版本:ServerBox v1.0.1617 (versionCode 161703)
- 安装来源:手动安装 APK(非商店,当前最新版本)

---

> 此条信息由 Hermes 代发(受 @bigmanBass666 委托提交)

Contributor guide

Open the contributing guide

Research direction

Start in lib/src/core/ext/ctx/dialog.dart at the _ImportFrom.clipboard branch, then inspect the import call in lib/view/page/backup.dart:586. Reproduce the backup snippet import with Chinese or other non-ASCII text, and verify valid UTF-8 clipboard JSON imports successfully while file and network imports remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
mobile
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
92/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.