Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have issues with the path on the getApplicationDocumentsDirectory() function

I get an error message over the path provider that says error: The getter 'path' isn't defined for the class 'Future'.

I am trying to generate a PDF file following the https://pub.dev/packages/pdf#-example-tab- and this example https://github.com/javico2609/flutter-challenges/blob/master/lib/pages/code_examples/pdf_and_csv/pdf.dart

But as I go on I get the error that path isn't defined on the Future. But as I see on the web I am doing it right. Here is the code:

final String dir = (getApplicationDocumentsDirectory()).path;
final String path = '$dir/receta.pdf';
final File file = File(path);
file.writeAsBytesSync(newpdf.save());

As I said. I can't run the app because I get the message error: The getter 'path' isn't defined for the class 'Future'.

Also tried to write

final Future<Directory> directory = getApplicationDocumentsDirectory();
final String dir = directory.path;
final String path = '$dir/receta.pdf';
final File file = File(path);
file.writeAsBytesSync(newpdf.save()); 

But it doesn't work, path on the variable dir shows the error

like image 453
Ricardo Chavarria Avatar asked Sep 07 '25 05:09

Ricardo Chavarria


2 Answers

In final Future<Directory> directory = getApplicationDocumentsDirectory(); getApplicationDocumentsDirectory() is any async function which means it will return the directory asynchronously , So then when you are trying to read directory.path;, directory is not initialized yet, its null.

Instead returning a future directory wait till it is initialized,

final Directory directory = await getApplicationDocumentsDirectory();
like image 161
Praneeth Dhanushka Fernando Avatar answered Sep 09 '25 02:09

Praneeth Dhanushka Fernando


Import this.. it solves the issue

 import 'package:path_provider/path_provider.dart';
like image 31
hayatbangash55 Avatar answered Sep 09 '25 02:09

hayatbangash55