dart Flutter pdf文件未保存并显示错误

pn9klfpd  于 2023-10-13  发布在  Flutter
关注(0)|答案(1)|浏览(126)

我试图简单地生成和保存pdf的问题是它在我的代码显示错误
我是这样做的

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'.

  },

插件版本
pdf:2.1.0 & path_provider:^2.0.2
我搜索每一个地方,但我没有找到任何解决方案不能找到它,所以为什么我得到这个错误-_-

fkaflof6

fkaflof61#

使用io library来写文件:

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());

  },

该错误可能是由于PDF库中定义的方法造成的。你可以使用默认的io库来解决这个问题。

相关问题