SoFunction
Updated on 2025-04-07

Flutter Dio Simple encapsulation demo

Flutter Dio Packaging

import 'package:dio/';
import 'package:path_provider/path_provider.dart';
import 'package:znw/app/net/url_config.dart';
class HttpClient {
  late Dio dio;
  static HttpClient instance = HttpClient._internal();
  ///The difference between factory constructor and ordinary constructor is that  ///The factory constructor can customize the instance creation process and return a new object or an existing object as needed.  factory HttpClient() {
    return instance;
  }
  HttpClient._internal() {
    print('Constructor.  .  .  .  .  .  .  .  .  .  .  .  .  .  ');
    dio = Dio();
     = UrlConfig.BASE_URL;
     = 8000;
    (LogInterceptor(responseBody: true)); // Output response content body  }
  /// get request  Future<Response?> get(String url, {Map<String, dynamic>? map}) async {
    try {
      var result = await (url, queryParameters: map);
      return result;
    } catch (e) {
      print('HttManager get e $e }');
      return null;
    }
  }
  ///post request  Future<Response?> post(String url, Map<String, dynamic>? map) async {
    try {
      var result = await (url, queryParameters: map);
      return result;
    } catch (e) {
      print('HttManager post e $e }');
      return null;
    }
  }
  ///Upload file fileName file name fileDir file path formData custom parameters  Future<Response?> uploadFile(String url, String? fileName, String fileDir,
      Map<String, dynamic> formData) async {
    try {
      formData['file'] =
          await (fileDir, filename: fileName);
      var response = await (url, data: formData);
      print();
      return response;
    } catch (e) {
      return null;
    }
  }
  ///Download file downLoadPath download path,  ///fileName The file name generated after downloading,  ///onProgress Download Progress  ///onFinish download completed  downLoad(
      String downLoadPath,
      String fileName,
      Function(int count, int total) onProgress,
      Function(String path) onFinish,
      Function(DioError e) onError) async {
    final directory = await getExternalStorageDirectory();
    String localPath = directory!.path;
    String savePath = "$localPath/$fileName";
    String apkUrl = downLoadPath;
    ///Parameter 1 File network storage URL    ///Parameter 2 Downloaded local directory file    ///Parameter 3 Download and listen    try {
      await (apkUrl, savePath,
          onReceiveProgress: (received, total) {
        if (total != -1) {
          ///% of current download percentage          print("${(received / total * 100).toStringAsFixed(0)}%");
          onProgress(received, total);
        }
      });
      onFinish(savePath);
      print(savePath);
    } on DioError catch (e) {
      //Exception handling code      onError(e);
    }
  }
}

The code explanation is in the comments~

The above is the detailed content of Flutter Dio simple packaging demo. For more information about Flutter Dio packaging, please follow my other related articles!