🚀 DevOps Certified Professional
📅 Starting: 1st of Every Month 🤝 +91 8409492687 | 🤝 +1 (469) 756-6329 🔍 Contact@DevOpsSchool.com

“[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type ‘Future‘ is not a subtype of type ‘File?’ in type cast” Error

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 3050): <!DOCTYPE html>
E/flutter ( 3050): ^
E/flutter ( 3050):
E/flutter ( 3050): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
E/flutter ( 3050): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9)
E/flutter ( 3050): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22)
E/flutter ( 3050): #3      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
E/flutter ( 3050): #4      JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 3050): #5      JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter ( 3050): #6      _MedicalRegistrationState.fetch_council
medical_registration_edit.dart:26
E/flutter ( 3050): <asynchronous suspension>
E/flutter ( 3050):


Solution:

change the code to this

Future<void> uploadImage(String title, File file) async {
var request =await http.MultipartRequest("POST", Uri.parse("https://api.imgur.com/3/image"));
request.fields["title"] = title;
request.headers["Authorization"] = "";
var picture = http.MultipartFile.fromBytes('image', (await rootBundle.load('assets/testimage.png')).buffer.asUint8List(),
filename: 'image.png');
request.files.add(picture);
var response = await request.send();
var responseData = await response.stream.toBytes();
var result = String.fromCharCodes(responseData);
print(result);
}

Use await change code from

final File? image = ImagePicker.pickImage(source: ImageSource.gallery) as File?;

TO

final XFile? image = await ImagePicker().pickImage(source: ImageSource.gallery);

Check documentation https://pub.dev/packages/image_picker

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x