I figured it out. We cannot use "File" because it use "dart.io" library which is not supported in Flutter WEB. So we have to use "Platform File" which use 'dart.html' library which is supported in Flutter WEB. Here is the code:
addStudent(
{@required token,
@required name,
@required email,
@required password,
@required rollNo,
@required phoneNo,
@required PlatformFile? image,
@required gender}) async {
http.MultipartRequest request = http.MultipartRequest(
"POST",
Uri.parse('Your URL HERE'),
);
request.headers['Authorization'] = 'Bearer $token';
request.fields['name'] = name;
request.fields['email'] = email;
request.fields['password'] = password;
request.fields['gender'] = gender;
request.fields['rollno'] = rollNo;
request.fields['phoneNumber'] = phoneNo;
// Here is the code to upload image to API
request.files.add(new http.MultipartFile(
'image', image!.readStream!, image.size,
filename: image.name));
//-------Send request
await request.send().then((value) async {
//------Read response
String result = await value.stream.bytesToString();
//-------Your response
print(result);
});
}