Slightly off-topic: Upload image picker file to Google Cloud SQL / AWS PostgresQL, after converting it to bytes
I can only help you on the side related to uploading image picker files converted to byte to PostgresQL.
I upload image as bytes to my PostgresQL DB, in the functions below I gather all the PickedFile and I upload them all at once while generating unique UID. Once they have been uploaded I save their uid to link them to the user who uploaded.
See the code below:
import 'dart:async'; import 'dart:io'; import 'dart:typed_data'; import 'dart:convert'; import 'package:image_picker/image_picker.dart'; import 'dart:math'; import 'package:postgres/postgres.dart'; import 'package:convert/convert.dart'; final Random _random = Random.secure(); // generate uid for your image String createCryptoSecureId([int length = 32]) { var values = List<int>.generate(length ~/ 1.3, (i) => _random.nextInt(256)); return base64Url.encode(values); } // works with list of PickedFile Future<String> uploadImagesToPSQL(final List<PickedFile> imageFileList) async { List<Uint8List> imageBytesList = []; for (PickedFile imageFile in imageFileList) { File file = File(imageFile.path); if (file != null) { Uint8List imageBytes = file.readAsBytesSync(); print(imageBytes); imageBytesList.add(imageBytes); } } String sqlQuery = "INSERT INTO myimages(id, image) VALUES (@id, decode(@image, 'hex'))"; PostgreSQLConnection connection = await initConnectionPSQL('update'); // can't share as there is sensitive code. List<String> imageIds; final futures = <Future>[]; for (var imageBytes in imageBytesList) { String imageId = createCryptoSecureId(32); imageIds.add(imageId); var substitutionValues = {"id": imageIds, "image": hex.encode(imageBytes)}; futures.add( connection.execute(sqlQuery, substitutionValues: substitutionValues)); } await Future.wait(futures); return imageIds.join(); }
NB: This is test code, ideally you want to use a REST api that has secure access to your DB.