Home > Back-end >  "[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<F
"[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<F

Time:07-16

I'm trying to upload a pic with Flutter and upload this image to imgur. But I am getting an error. Before that, let me show you my codes:

FloatingActionButton.large(
  child: Icon(Icons.folder, color: Colors.white, size: 45,),
  backgroundColor: Color.fromARGB(255, 36, 36, 36),
  onPressed: () async {
    final File? image = ImagePicker.pickImage(source: ImageSource.gallery) as File?;
    final File? imagefile = File(image!.path);
    if (imagefile == null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("not image selected."),
          backgroundColor: Color.fromARGB(255, 36, 36, 36),
        ),
      );
      return null;
    }
    else {
      print("else");
      uploadImage("bolat", image);
    }
  },
),
// ...
  void uploadImage(String title, File file) async {
    var request = 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);
 }

When I try this code I get this error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<File>' is not a subtype of type 'File?' in type cast
#0      _BolatAktarAppState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:bolataktar_resimyukleme/screens/resimyukleme_main.dart:66:104)
#1      _BolatAktarAppState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:bolataktar_resimyukleme/screens/resimyukleme_main.dart:65:42)
#2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:1010:21)
#3      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:247:24)
#4      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:620:11)
#5      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:299:5)
#6      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:269:7)
#7      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:163:27)
#8      GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:454:20)
#9      GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:430:22)
#10     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:337:11)
#11     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:385:7)
#12     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:347:5)
#13     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:304:7)
#14     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:285:7)
#15     _invoke1 (dart:ui/hooks.dart:167:13)
#16     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:341:7)
#17     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method pickImage on channel plugins.flutter.io/image_picker)
#0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:292:7)
<asynchronous suspension>
#1      ImagePicker.pickImage (package:image_picker/image_picker.dart:64:25)
<asynchronous suspension>

How can I solve this problem? Thanks in advance for your help.

CodePudding user response:

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);
     }

CodePudding user response:

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

  • Related