I am attempting to write a custom action to create a PDF i am getting error messages such as Action declaration not found, action cannot be parsed or sometimes it will let me save and give me the message " Parameters in the settings don't match the parameters in the code editor If you confirm we will replace....". If i choose to proceed it wipes out all my defined arguments shown in the screen shot. I have tried various code variations and receive the same types of errors. I have even resorted to AI still no joy. Also i am not sure what to set as return argument when creating a PDF perhaps that might be part of the issue. I was under the impression that if i defined a field in the code eg pumpName then it should be defined as an argument on the right hand hand side. My current code is below. Is someone able to shed some light on where this is all going wrong? My level of frustration with action code editor is going through the roof.
// Automatic FlutterFlow imports
import '/backend/schema/structs/index.dart';
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 'package:google_sign_in/google_sign_in.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:share_plus/share_plus.dart';
Future insertServiceHistoryintoPDF() async {
Future<void> insertServiceHistoryintoPDF(
List<ServiceHistory> serviceHistory,
String pumpName,
String companyName,
String serial,
String registration,
) async {
// Create a PDF document
final document = pw.Document();
// Add a header
document.add(pw.Container(
alignment: pw.Alignment.center,
child: pw.Text(
'Service History',
style: pw.TextStyle(
fontSize: 18,
fontWeight: pw.FontWeight.bold,
),
),
));
// Add a table header
document.add(pw.Table(
border: pw.Border.all(color: PdfColors.grey),
children: [
pw.TableRow(children: [
pw.Text('Pump:', style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text(pumpName,
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Pump Owner:',
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text(companyName,
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
]),
pw.TableRow(children: [
pw.Text('Serial No:',
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text(serial, style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Registration Number:',
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text(registration,
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
]),
pw.TableRow(children: [
pw.Text('Date', style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Service Type',
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Service Provider',
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Kms', style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Hours', style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
pw.Text('Remarks',
style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
])
],
));
// Add table rows for each service history record
for (final service in serviceHistory) {
document.add(pw.TableRow(children: [
pw.Text(service.date),
pw.Text(service.serviceType),
pw.Text(service.serviceProvider),
pw.Text(service.kms.toString()),
pw.Text(service.hours.toString()),
pw.Text(service.remarks),
]));
}
// Generate a dynamic filename
final currentDate = DateTime.now();
final filename =
'Service History Report for ${pumpName} - ${registration} - ${currentDate.toIso8601String()}.pdf';
// Save the PDF to a temporary file
final tempDir = await getTemporaryDirectory();
final tempFile = File('${tempDir.path}/$filename');
await tempFile.writeAsBytes(await document.save());
// Provide options to the user
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Share PDF'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () async {
await OpenFile.open(tempFile.path);
},
child: Text('Open'),
),
TextButton(
onPressed: () async {
await Share.shareFiles([tempFile.path],
text: 'Service History PDF');
},
child: Text('Share'),
),
TextButton(
onPressed: () async {
// Assuming you have Google Sign-in set up
final googleSignIn = GoogleSignIn();
await googleSignIn.signIn();
final googleDrive = GoogleDrive(googleSignIn.currentUser!);
await googleDrive.uploadFile(
tempFile.path, 'service_history.pdf');
},
child: Text('Save to Google Drive'),
),
],
),
),
);
}
}