From f831fd1405c48a395c8902ba92938c8045d5e33c Mon Sep 17 00:00:00 2001 From: peima Date: Fri, 24 Jul 2026 20:17:07 +0800 Subject: [PATCH] phase3: util_web null-safe (showImage, WebImagePicker, ImageInfo) util_io deferred (excluded from web build via conditional import). --- lib/utils/util_web.dart | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/utils/util_web.dart b/lib/utils/util_web.dart index 9632234..0593658 100644 --- a/lib/utils/util_web.dart +++ b/lib/utils/util_web.dart @@ -41,21 +41,21 @@ class Util { return box; } - static Widget showImage(String imageUrl, {double width, double height, - BoxFit fit, Widget Function(BuildContext, String, dynamic) errorWidget}) { + static Widget showImage(String imageUrl, {double? width, double? height, + BoxFit? fit, Widget Function(BuildContext, String?, dynamic)? errorWidget}) { return Image.network(imageUrl, fit: fit, width: width, height: height, cacheWidth: width != null ? width.round() : null, cacheHeight: height != null ? height.round() : null, - loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent loadingProgress) { + loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) { if (loadingProgress == null) return child; return Center( child: Utils.imageLoadingIndicator(), ); }, - errorBuilder: (BuildContext context, Object object, StackTrace stackTrace) { + errorBuilder: (BuildContext context, Object object, StackTrace? stackTrace) { print('StackTrace: $stackTrace'); if (errorWidget != null) { return errorWidget(context, null, null); @@ -105,16 +105,16 @@ class Util { } void startFilePicker(BuildContext context, User user, {int commentId = -1, int orderId = 0}) async { - InputElement uploadInput = FileUploadInputElement(); + FileUploadInputElement uploadInput = FileUploadInputElement(); uploadInput.click(); uploadInput.onChange.listen((e) { final files = uploadInput.files; - if (files.length >= 1) { + if (files != null && files.length >= 1) { final File file = files[0]; final FileReader reader = new FileReader(); reader.onLoadEnd.listen((e) { - uploadPicture(context, reader.result, file.name, user, commentId: commentId, orderId: orderId); + uploadPicture(context, reader.result as Uint8List, file.name, user, commentId: commentId, orderId: orderId); }); reader.onError.listen((e) { Fluttertoast.showToast( @@ -209,11 +209,11 @@ class Util { void startFilePicker2(BuildContext context, int imageId, OnGotFile onGotFile) async { final _image = await WebImagePicker().pickImage(); - onGotFile(imageId, _image.dataScheme); + onGotFile(imageId, _image?.dataScheme ?? ''); } void createTicket(BuildContext context, String msg, List> images, - OnSuccess onSuccess, OnError onError, {int id}) { + OnSuccess onSuccess, OnError onError, {int? id}) { var formData = FormData(); formData.fields.add(MapEntry("msg", msg)); formData.fields.add(MapEntry('id', id == null ? '0' : id.toString())); @@ -291,32 +291,32 @@ class Util { } class ImageInfo { - String name; - String data; - String dataScheme; - String path; + String? name; + String? data; + String? dataScheme; + String? path; } class WebImagePicker { - Future pickImage() async { + Future pickImage() async { print('pickImage'); final ImageInfo data = ImageInfo(); final FileUploadInputElement input = FileUploadInputElement(); - document.body.children.add(input); + document.body?.children.add(input); input..accept = 'image/*'; input.click(); await input.onChange.first; - if (input.files.isEmpty) return null; + if (input.files == null || input.files!.isEmpty) return null; final reader = FileReader(); - reader.readAsDataUrl(input.files[0]); + reader.readAsDataUrl(input.files![0]); await reader.onLoad.first; final encoded = reader.result as String; // remove data:image/*;base64 preambule final stripped = encoded.replaceFirst(RegExp(r'data:image/[^;]+;base64,'), ''); //final imageBase64 = base64.decode(stripped); - final imageName = input.files?.first?.name; - final imagePath = input.files?.first?.relativePath; + final imageName = input.files?[0].name; + final imagePath = input.files?[0].relativePath; data.name = imageName; data.data = stripped; data.dataScheme = encoded;