I have implemented a custom function named urlToFile in my project, but I'm encountering a compilation error when building for the web. Below, I've provided the code for the urlToFile function and the error message that I receive during the web compilation process.
Code for urlToFile Function:
import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
import 'package:my_project_path/flutter_flow/uploaded_file.dart'; // Note: I'm unsure of the exact project path here
class FFUploadedFile {
String name;
Uint8List bytes;
String? uploadUrl; // To be filled in after the file is uploaded
FFUploadedFile({
required this.name,
required this.bytes,
this.uploadUrl,
});
}
Future<FFUploadedFile> urlToFile(String? url) async {
if (url == null || url.isEmpty) {
throw Exception('The provided URL is null or empty.');
}
final response = await http.get(Uri.parse(url));
if (response.statusCode != 200) {
throw Exception('Error downloading the file: ${response.statusCode}');
}
final tempDir = await getTemporaryDirectory();
final filePath = '${tempDir.path}/${Uri.parse(url).pathSegments.last}';
final file = File(filePath);
await file.writeAsBytes(response.bodyBytes);
final ffUploadedFile = FFUploadedFile(
name: Uri.parse(url).pathSegments.last,
bytes: response.bodyBytes,
);
return ffUploadedFile;
}
Web Compiler Error:
Error: Command failed: flutter build web --web-renderer html --no-pub --no-version-check --no-tree-shake-icons
Target dart2js failed: ProcessException: Process exited abnormally:
lib/pages/chat/chat_widget.dart:8042:79:
Error: A value of type 'FFUploadedFile/*1*/' can't be assigned to a variable of type 'FFUploadedFile/*2*/?'.
- 'FFUploadedFile/*1*/' is from 'package:chat_fire_copia/custom_code/actions/url_to_file.dart' ('lib/custom_code/actions/url_to_file.dart').
- 'FFUploadedFile/*2*/' is from 'package:chat_fire_copia/flutter_flow/uploaded_file.dart' ('lib/flutter_flow/uploaded_file.dart').
await actions.urlToFile(
^
Error: Compilation failed.I am unsure of the exact path to include for the FFUploadedFile import in my project, and this might be causing the issue. Any guidance on how to properly reference the FFUploadedFile class or adjustments needed in the urlToFile function would be greatly appreciated.