Hi guys!
I asked ChatGPT to generate the following code to allow users to tap a widget button and then save a PDF from a URL locally on their devices.
The download starts normally but is shortly followed by a "Failed" message.
ChatGPT told me that the code seems all right, so what are the issues here, guys?
// 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!
// Importe os pacotes necessários
import 'package:flutter_downloader/flutter_downloader.dart';
Future<void> downloadPdf() async {
final String pdfUrl = 'https://www.family-action.org.uk/content/uploads/2019/07/meals-more-recipes.pdf';
final taskId = await FlutterDownloader.enqueue(
url: pdfUrl,
savedDir: 'Download/',
showNotification: true,
openFileFromNotification: true,
);
FlutterDownloader.registerCallback((id, status, progress) {
// Adicione o código de acompanhamento do download, se necessário
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(
debug: true, // configurar para false em produção
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyPDFDownload(),
);
}
}
class MyPDFDownload extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PDF Download Example'),
),
body: Center(
child: ElevatedButton(
onPressed: downloadPdf,
child: Text('Download PDF'),
),
),
);
}
}