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.

1 Answer 1

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);
    });
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

it says: The method 'basename' isn't defined for the type '_HowWillUseState'. (Documentation)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.