Files
flutterApp/lib/core/utils/image_url_util.dart

25 lines
914 B
Dart
Raw Normal View History

import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
/// 图片地址工具:后端返回的图片路径可能是相对路径(如 /profile/avatar/xxx.jpg),
/// 直接交给 Image.network 会因缺少 host 抛 "No host specified in URI" 异常。
/// 此工具负责将相对路径拼接成完整的服务器地址。
class ImageUrlUtil {
ImageUrlUtil._();
/// 补全图片地址
/// - 已是完整 http(s) 地址:原样返回
/// - 以 / 开头的相对路径:拼接 baseUrl
/// - 其他相对路径:拼接 baseUrl + /
/// - null 或空:返回 null
static String? resolve(String? path) {
if (path == null || path.isEmpty) return null;
if (path.startsWith('http://') || path.startsWith('https://')) {
return path;
}
if (path.startsWith('/')) {
return '${HttpApiConsts.baseUrl}$path';
}
return '${HttpApiConsts.baseUrl}/$path';
}
}