Skip to content

Instantly share code, notes, and snippets.

@Klerith
Created April 28, 2025 18:05
Show Gist options
  • Save Klerith/ff18f7a04c7717e0db6709969c379f8c to your computer and use it in GitHub Desktop.
Save Klerith/ff18f7a04c7717e0db6709969c379f8c to your computer and use it in GitHub Desktop.
Esta clase toma un URL, lo descarga y crea un XFile
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
import 'package:uuid/uuid.dart';
// path_provider
class TemporalImages {
static Future<XFile> xFileFromUrl(String imageUrl) async {
final uuid = Uuid();
final tempDir = await getTemporaryDirectory();
final filePath = '${tempDir.path}/${uuid.v4()}.png';
final dio = Dio();
final response = await dio.get<List<int>>(
imageUrl,
options: Options(responseType: ResponseType.bytes),
);
final file = File(filePath);
await file.writeAsBytes(response.data!);
return XFile(file.path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment