fix(client): 修复 web 端选择文件后无反应问题
- 添加 result.files.isEmpty 检查(web file_picker 偶发空列表) - web 平台不传 initialDirectory(避免潜在兼容性问题) - 添加 mounted 检查和 try-catch,异常通过 SnackBar 显示 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -1460,29 +1461,39 @@ class _BatchImportWidgetState extends ConsumerState<_BatchImportWidget> {
|
||||
}
|
||||
|
||||
Future<void> _pickFile(int index) async {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['xls', 'xlsx'],
|
||||
withData: true,
|
||||
initialDirectory: _lastDir,
|
||||
);
|
||||
if (result == null) return;
|
||||
try {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['xls', 'xlsx'],
|
||||
withData: true,
|
||||
initialDirectory: kIsWeb ? null : _lastDir,
|
||||
);
|
||||
if (result == null || result.files.isEmpty) return;
|
||||
|
||||
// 保存目录供下次使用
|
||||
final path = result.files.first.path;
|
||||
if (path != null) {
|
||||
final dir = path.contains('/') ? path.substring(0, path.lastIndexOf('/')) : null;
|
||||
if (dir != null) {
|
||||
_lastDir = dir;
|
||||
SharedPreferences.getInstance().then((p) => p.setString(_prefKey, dir));
|
||||
// 保存目录供下次使用(仅非 web 平台)
|
||||
if (!kIsWeb) {
|
||||
final path = result.files.first.path;
|
||||
if (path != null) {
|
||||
final dir = path.contains('/') ? path.substring(0, path.lastIndexOf('/')) : null;
|
||||
if (dir != null) {
|
||||
_lastDir = dir;
|
||||
SharedPreferences.getInstance().then((p) => p.setString(_prefKey, dir));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_slots[index].file = result.files.first;
|
||||
_slots[index].success = null;
|
||||
_slots[index].error = null;
|
||||
});
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_slots[index].file = result.files.first;
|
||||
_slots[index].success = null;
|
||||
_slots[index].error = null;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('选择文件失败:$e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _runImport() async {
|
||||
|
||||
Reference in New Issue
Block a user