0

I am trying to upload an image with user data into a PHP SQL server using laravel API..

but I am getting error on uploading image..

My Image Code is:

var multipartFile = http.MultipartFile(
  'image',
  image.readAsBytes().asStream(),
  image.lengthSync(),
  filename: image.path.split('/').last,
);

Error I am getting Is :

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'MultipartFile'

I have also tries to upload image directly without multiparts but still getting error.

Ehsan Abid
  • 43
  • 6

1 Answers1

1

try this

import 'dart:convert';
import 'dart:io';

import 'package:async/async.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';

class ApiHelper {
  imageUpload(File imageFile) async {
    var stream = http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    var length = await imageFile.length();

    var uri = Uri.parse(uploadURL);

    var request = http.MultipartRequest("POST", uri);
    var multipartFile = http.MultipartFile('file', stream, length, filename: basename(imageFile.path));

    request.files.add(multipartFile);
    var response = await request.send();
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
    });
  }
}
K K Muhammed Fazil
  • 668
  • 1
  • 5
  • 12