2

I've created a flutter application for android and web. In Flutter web, I tried to upload image to server just like it works with firebase. But it is not working somehow. I've seen some solutions for this task. But I wonder, what is actually wrong with my code.

final url = Uri.parse('$apiHeader/poststore');
String token = await getUserToken();

Map<String, String> headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer $token'
};

var request = http.MultipartRequest("POST", url);


request.headers.addAll(headers);

request.fields['category_id'] = model.categoryId;
request.fields['title'] = model.title;


//I want to know about this section of the code, how can i make it work
if (kIsWeb) {
  final fileBytes =
      await model.image.readAsBytes(); // convert into bytes

  var multipartFile = http.MultipartFile.fromBytes(
      'fileName[]', fileBytes); // add bytes to multipart

  request.files.add(multipartFile);
} else {
  var multipartFile = await http.MultipartFile.fromPath(
      'fileName[]', model.image.path);

  request.files.add(multipartFile);
}

var response = await request.send();

0 Answers0