I am trying to post files (html files since working on web) to the API.
I am getting an error saying Null
is not a subtype of type InstanceRef
in type cast.
I don’t know what is causing this. Everything seems to be fine to me in the code.
Here is the code:
uploadFiles() async {
///
try {
var token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIyIiwibmJmIjoxNjc5MzgzMTY5LCJleHAiOjE2NzkzODQ5NjksImlhdCI6MTY3OTM4MzE2OX0.HL5Y39p0cxpbuOhbls-hjq2Nu_xnVGAJ4qacESp1yNw';
var url =
Uri.parse("my_url");
var request = http.MultipartRequest('POST', url);
Map<String, String> headers = {
"Authorization": "Bearer $token",
"Content-type": "multipart/form-data"
};
request.headers.addAll(headers);
request.fields
.addAll({"facility_id": "46", "module_id": "3263", "id": "1"});
for (var file in files) {
var reader = html.FileReader();
reader.readAsArrayBuffer(file);
var completer = Completer<List<int>>();
bool _isCompleted = false; // add this flag
reader.onLoadEnd.listen((event) {
if (!_isCompleted) {
// check if already completed
_isCompleted = true;
completer.complete(reader.result as List<int>);
}
});
var filename = file.name;
var multipartFile = http.MultipartFile.fromBytes(
'file',
(await completer.future),
filename: filename,
);
request.files.add(multipartFile);
}
var response = await request.send();
if (response.statusCode == 200) {
print('Upload successful');
} else {
print('Upload failed');
}
} //
catch (e) {
print(e);
}
}
4
Answers
This is what finally worked for me:
Turns out content-length was required.
for file upload you should send with
"Content-type": "multipart/form-data"
there are two possible way
In this you upload file using fileupload!.files.single.path,
The Other way is using Base64
In this you call api normally
Very Simple solutions
Add and import library