方法1
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';Future<File> _fileFromImageUrl() async {final response = await http.get(Uri.parse('https://example.com/xyz.jpg)');final documentDirectory = await getApplicationDocumentsDirectory();final file = File(join(documentDirectory.path, 'imagetest.png'));file.writeAsBytesSync(response.bodyBytes);return file;}
方法2
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';Future<File> getImage({required String url}) async {/// Get Image from serverfinal Response res = await Dio().get<List<int>>(url,options: Options(responseType: ResponseType.bytes,),);/// Get App local storagefinal Directory appDir = await getApplicationDocumentsDirectory();/// Generate Image Namefinal String imageName = url.split('/').last;/// Create Empty File in app dir & fill with new imagefinal File file = File(join(appDir.path, imageName));file.writeAsBytesSync(res.data as List<int>);return file;
}
方法3
Future<File> fileFromImageUrl() async {
String img='https://pps.whatsapp.net/v/t61.24694-24/160266256_1115367465775510_3546030680878251116_n.jpg?ccb=11-4&oh=01_AdSsrMGOPfs8CUJsEkYImMUu5L4DAzt2ym8eBrdsMG5O0Q&oe=63D7B45E';final response = await http.get(Uri.parse(img));final documentDirectory = await getApplicationDocumentsDirectory();final file =File(p.join(documentDirectory.path, 'File Name.jpg'));file.writeAsBytesSync(response.bodyBytes);return file;}