Getting the Filename from the Uploaded file to Supabase bucket instead of the full URL.

Resolved

Here's the code. It works perfectly, if you have any specific doubt about any function, just ask ChatGPT, cause it almost entirely made by him.

Please pay attention I use to functions, one only to display the right name without names via generated children, and the second one that goes to Supabase storage with a random code to avoid repetition.

I also made a second code to delete the specific attachment at Supabase.

// Automatic FlutterFlow imports
import '/backend/supabase/supabase.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:math';
import 'package:file_picker/file_picker.dart';

// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the green button on the right!import 'package:supabase/supabase.dart';

Future<void> documentPicker() async {
  final selectedFiles =
      await FilePicker.platform.pickFiles(allowMultiple: true);

  if (selectedFiles != null) {
    var selectedUploadedFiles = <FFUploadedFile>[];

    String getFileExtension(String fileName) {
      final dotIndex = fileName.lastIndexOf('.');
      if (dotIndex != -1) {
        return fileName.substring(dotIndex + 1);
      }
      return '';
    }

// Usando a função para obter a extensão do arquivo
    for (var file in selectedFiles.files) {
      final fileName = file.name;
      final fileExtension = getFileExtension(fileName);
      final fileNameWithoutExtension =
          fileName.replaceAll('.$fileExtension', '');
      final bytes = file.bytes;

      // Use fileExtension conforme necessário

      // Gerar um número aleatório para adicionar ao nome do arquivo
      final randomSuffix = generateRandomNumber();

      // Adicionar o nome do arquivo com o prefixo aleatório ao estado do aplicativo
      final newFileName =
          '$fileNameWithoutExtension-$randomSuffix.$fileExtension';
      FFAppState().anexosExames.add(fileName!);
      FFAppState().anexosExamesSupabaseNome.add(newFileName);

      // Adicionar o arquivo à lista para uso posterior
      selectedUploadedFiles.add(FFUploadedFile(
        name: newFileName,
        bytes: bytes,
      ));
    }

    // Fazer upload dos arquivos para o Supabase Storage
    for (var uploadedFile in selectedUploadedFiles) {
      final storage = Supabase.instance.client.storage;
      await storage
          .from('bucket')
          .uploadBinary('anexos/${uploadedFile.name}', uploadedFile.bytes!);
    }
  }
}

int generateRandomNumber() {
  var now = DateTime.now();
  var rand = Random(now.millisecondsSinceEpoch);
  return rand.nextInt(100000); // Número aleatório entre 0 e 99999
}

How to delete the attachment:

// Automatic FlutterFlow imports
import '/backend/supabase/supabase.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!

Future<void> deleteAttachment(int index) async {
  final fileName = FFAppState().anexosExamesSupabaseNome[index];

  // Deletar arquivo do Supabase Storage
  await Supabase.instance.client.storage
      .from('bucket')
      .remove(['anexos/$fileName']);
  FFAppState().anexosExames.removeAt(index);
  FFAppState().anexosExamesSupabaseNome.removeAt(index);
  // Se o arquivo foi deletado com sucesso, remover da lista no AppState
}


Initial Content:

Please someone help me, it's been 2 days and I still cant find how to do it. All documentation around is about firebase, and even ChatGPT isnt helping me getting this file name from Supabase.

4
2 replies