upload method

Future<List<FileInfo?>> upload(
  1. List<XFile> files, {
  2. dynamic onSent(
    1. XFile file,
    2. int sent,
    3. int total
    )?,
  3. dynamic onFail(
    1. XFile file,
    2. int code,
    3. String info
    )?,
  4. dynamic onSucess(
    1. FileInfo file
    )?,
})

Implementation

Future<List<FileInfo?>> upload(
  List<XFile> files, {
  Function(XFile file, int sent, int total)? onSent,
  Function(XFile file, int code, String info)? onFail,
  Function(FileInfo file)? onSucess,
}) async {
  List<Future> fs = [];
  List<FileInfo?> infos = [];
  locate(XFile file) async {
    var id = await fileManager.createFileId();
    if (id == null) {
      if (onFail != null) onFail(file, 1, "can't create file id");
      return;
    }
    var info = await _upload(id, file, onSent, (err) {
      if (onFail == null) {
        if (kDebugMode) {
          print(err);
        }
      }
      if (err is DioException) {
        if (err.response == null) {
          onFail!(file, 2, err.message ?? "unkwon error");
          return;
        }
        onFail!(file, err.response!.statusCode ?? 500,
            err.response!.data ?? "unkown error");
        return;
      }
      onFail!(file, 3, "$err");
    });
    if (info == null) {
      return;
    }
    infos.add(info);
    if (onSucess != null) {
      onSucess(info);
    }
  }

  for (var file in files) {
    fs.add(locate(file));
  }
  await Future.wait(fs);
  return infos;
}