โœ… Upload Multiple images from (local state widget ) to a certain path in firebase

 // Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'dart:typed_data';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_storage/firebase_storage.dart';

Future<String> actionNameHERE(
    String userId, List<FFUploadedFile> TheNameOfTheWidgetState) async {
  if (userId.isEmpty) {
    throw ArgumentError("userId must be provided.");
  }

  if (Firebase.apps.isEmpty) {
    await Firebase.initializeApp();
  }

  String downloadURLs = '';

  for (var uploadedFile in TheNameOfTheWidgetState) {
    if (uploadedFile.bytes == null) {
      continue; // If there are no bytes, skip this file
    }
    Uint8List imageData =
        uploadedFile.bytes!; // Asserting that bytes is not null
    String? fileName = uploadedFile.name;
    if (fileName == null) {
      continue; // If there is no fileName, skip this file
    }
    String mimeType = 'image/' + fileName.split('.').last;
    String storagePath = 'users/$userId/small';

    Uri storageUri = Uri.parse(storagePath);
    String fullPath = storageUri.path + '/' + fileName;

    FirebaseStorage storage =
        FirebaseStorage.instanceFor(bucket: 'gs://YOUR-APP-NAME');
    Reference storageReference = storage.ref().child(fullPath);

    SettableMetadata metadata = SettableMetadata(contentType: mimeType);

    UploadTask uploadTask = storageReference.putData(imageData, metadata);
    TaskSnapshot taskSnapshot = await uploadTask;

    String downloadURL = await taskSnapshot.ref.getDownloadURL();
    downloadURLs += downloadURL + '\\n';
  }

  return downloadURLs;
}












This is a custom action for uploading images to firebase i needed to upload the images to a local state so i can see them before uploading to a specific path in firebase
- ($userid ) is a an argument that will name the folder based on the user id
- Change the bucket to your firebase storge url

-Change the storge path to your desired path (Example: users/$userId/small)

this is the name of the widget that you need to assign

  • next : the (Done button) is where you would upload your images to firebase(via custom action )
    userid
    and Uploaded list

9
1 reply