i am coding this new Ecommerce App where the user should add products, so i used Multi_Image_picker package where it let him choose multiple images and make them a List...
so i searched google how to convert this list to a file list and found a way i am using in my database code where i want to send all of this files to laravel backend:
// String Prodcut Url/
final productUrl =
MyApp.defaultUrl + "/api/ecommerce/products/store?api_token=$token";
// Get Dio
var dio = Dio();
try {
List<MultipartFile> allMultiFiles = [];
for (Asset asset in images) {
ByteData byteData = await asset.getByteData();
List<int> imageData = byteData.buffer.asUint8List();
var multipartFile = new MultipartFile.fromBytes(
imageData,
filename: basename(asset.name),
);
allMultiFiles.add(multipartFile);
}
var productData = FormData.fromMap({
'store_id': storeId,
'name': productName,
'description': description,
'category_id': categoryId,
'price': price,
'currency': currency,
'is_discount': isDiscount,
'images': allMultiFiles,
});
print(productData.files);
var response = await dio.post(productUrl, data: productData);
// Decoding The Request
var responseArray = jsonDecode(response.data);
print(responseArray);
} catch (e) {
// Returning error if exist
print(e);
return e;
}
There is more code up there but for the space..
its giving me this reply
I/flutter (16670): FormatException: Unexpected character (at character 1)
I/flutter (16670): Illuminate\Http\UploadedFile Object
I/flutter (16670): ^
welp my php code is:
public function store(Request $request) {
$validated = $request->validate([
'store_id' => 'required',
'name' => 'required',
'description' => 'required',
'price' => 'required',
'currency' => 'required',
// 'images' => 'required',
]);
$images_path = array();
if ($request->file('images')) {
foreach ($request->file('images') as $file) {
$images_path[] = $file->store('public/uploads/products_images');
}
}
$product = Products::create([
'store_id' => $request->input('store_id'),
'name' => $request->input('name'),
'description' => $request->input('description'),
'category_id' => $request->input('category_id'),
'price' => $request->input('price'),
'currency' => $request->input('currency'),
'images' => json_encode($images_path),
'is_discount' => $request->input('is_discount'),
]);
return json_encode(['success' => $product]);
}
so if someone can help me to understand why this error is happening here