Send Image api rest

 import 'dart:async';

import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:womenentrepreneur/configs/app_urls.dart';
import 'package:womenentrepreneur/models/fair/fair_application_mod.dart';

Future<FairApplicationModel> fairApplicationService(
{required String token,
required int fairId,
required String companyName,
required String companyAddress,
required String companyPhone,
required String companyWebsite,
required String district,
required String postCode,
required File photoFile,
required List<int> selectStallIds}) async {
debugPrint("call fairApplicationService");
FairApplicationModel response = FairApplicationModel();
final Map<String, String> header = {
HttpHeaders.authorizationHeader: 'Bearer $token'
};

try{

final fairApplicationFormJson =
{
"fair_id": fairId.toString(),
"company_name": companyName,
"address": companyAddress,
"district": district,
"postal_code": postCode,
"web_address": companyWebsite,
"telephone": companyPhone,
"stalls": selectStallIds.toString(),
};

final request = http.MultipartRequest('POST', Uri.parse(AppUrls.fairApplication))
..headers.addAll(header)..fields.addAll(fairApplicationFormJson);

final photoMultipartFile =
await http.MultipartFile.fromPath('company_logo', photoFile.path);
request.files.add(photoMultipartFile);



// final fairApplicationFormField =
// http.MultipartFile.fromString('traineeList', fairApplicationFormJson);
// request.files.add(fairApplicationFormField);

final response3 = await request.send();
final body = await response3.stream.bytesToString();

final response2 = fairApplicationModelFromJson(body);
final success = response2.success;
if (response3.statusCode == 200 && success != null && success) {
debugPrint('Payload sent successfully!');
return response2;
} else {

debugPrint('response body: $body');
return response2;
}
}on TimeoutException catch (e) {
debugPrint("TimeoutException in fairApplicationService: $e");
response.success = false;
response.message = "Time out";
debugPrint(
"responseModel success: ${response.success}\nresponseModel message: ${response.message}");
return response;
} on SocketException catch (e) {
debugPrint("SocketException in fairApplicationService: $e");
response.success = false;
response.message = "Internet connection failed";
debugPrint(
"responseModel success: ${response.success}\nresponseModel message: ${response.message}");
return response;
}
catch(e){
response.success = false;
response.message = "Something went wrong";
debugPrint(
"responseModel success: ${response.success}\nresponseModel message: ${response.message}");
return response;
}
}





Comments