1

I am trying to simple generate and save pdf issue is its showing error in my code

I am doing like this

  import 'package:path_provider/path_provider.dart';
  import 'package:pdf/pdf.dart';
  import 'package:pdf/widgets.dart' as pw;

  onTap: () async{
    final pdf = pw.Document();

    pdf.addPage(
      pw.Page(
        build: (pw.Context context) => pw.Center(
          child: pw.Text('Hello World!'),
        ),
      ),
    );
    // Share.shareFiles([pdf], text: 'Reports');

    final output = await getTemporaryDirectory();
    final path = "${output.path}/temp.pdf";
    final file = File(path); // here its showing error 2 positional 
    await file.writeAsBytes(pdf.save()); // here its showing The method 'writeAsBytes' isn't defined for the type 'File'.

  },

Plugin versions

pdf: 2.1.0 & path_provider: ^2.0.2

I search every where but I didnt find any solution cant find it so why I am getting this error -_-

5
  • 1
    can you please add your import statements to code Commented May 29, 2021 at 3:23
  • @RushikeshTalokar added Commented May 29, 2021 at 3:33
  • Same issue I also try to update plugin but this error is not going Commented May 29, 2021 at 3:35
  • @UmaizKhan hi, did u find a solution for this problem. I Commented May 26, 2023 at 9:02
  • @UmaizKhan I'm kinda stuck there and I couldn't figure out how to deal with it, I tried both of these packages pdf/[syncfusion_flutter_pdf] (pub.dev/packages/syncfusion_flutter_pdf) and problem didn't disappear + there is no error shown while running the code Commented May 26, 2023 at 9:17

1 Answer 1

1

Use io library to write the file:

    import 'dart:io' as io;
    
    onTap: () async{
    final pdf = pw.Document();

    pdf.addPage(
      pw.Page(
        build: (pw.Context context) => pw.Center(
          child: pw.Text('Hello World!'),
        ),
      ),
    );
    // Share.shareFiles([pdf], text: 'Reports');

    //replace your code to save file from bellow
    final output = await getTemporaryDirectory();
    final path = "${output.path}/temp.pdf";
    final file = await io.File(path).writeAsBytes(pdf.save());


  },

The error would be due to methods defined in the pdf library. You can use default io library to solve that.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.