Hello all,
I am not able to upload the generated pdf to Firebase Storage while testing from Android/iOS device. It is working properly from Web. I tried changing the storage rules to allow all access still it failed. I am now struck and if anyone can guide me with a solutions.
Thank you.
Here is the code:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.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 'package:cloud_firestore/cloud_firestore.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:firebase_auth/firebase_auth.dart';
import 'package:intl/intl.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
Future<bool> generateStatementAndUploadToStorage(
DateTime fromDate, DateTime toDate, String selectedNickname) async {
try {
final User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
throw Exception('User is not logged in.');
}
final String userId = user.uid;
// Create a simple PDF with "Hello" message
final pdf = pw.Document();
pdf.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(child: pw.Text('Hello'));
},
));
// Save the PDF to Firebase Storage
final bytes = await pdf.save();
final String pdfFileName =
'generated_pdf_${DateTime.now().millisecondsSinceEpoch}.pdf';
// Reference to the user's folder in Firebase Storage
final firebase_storage.Reference storageReference =
firebase_storage.FirebaseStorage.instance
.ref('users/$userId/pdfs/$pdfFileName');
// Upload the PDF file
await storageReference.putData(bytes);
print('PDF uploaded to Firebase Storage: $pdfFileName');
// Records found
return false;
} catch (error) {
print('Error fetching data: $error');
// Error occurred
return false;
}
}