我的 Pdf 文档没有下载或打开,因为图像文件在 flutter 下载后打开

My Pdf document is not downloading or opening where as image file is opening after download in flutter

我的 Pdf 文档无法下载或打开,而图像文件在 flutter 中下载后打开。

我已经使用文件选择器选择图像文件和 pdf 文件并将它们发送到 firebase 存储。

我的图像文件正在下载和打开,但在将它发送到 firebase 存储后无法下载 PDF 文件。

可能是正在下载,但是点击下载按钮无法查看

   Future<void> downloadFile(StorageReference ref) async {
    final String url = await ref.getDownloadURL();
    final http.Response downloadData = await http.get(url);
    final Directory systemTempDir = Directory.systemTemp;
    final File tempFile = File('${systemTempDir.path}/tmp.jpg');
    if (tempFile.existsSync()) {
      await tempFile.delete();
    }
    await tempFile.create();
    final StorageFileDownloadTask task = ref.writeToFile(tempFile);
    final int byteCount = (await task.future).totalByteCount;
    var bodyBytes = downloadData.bodyBytes;
    final String name = await ref.getName();
    final String path = await ref.getPath();
    print(
      'Success!\nDownloaded $name \nUrl: $url'
          '\npath: $path \nBytes Count :: $byteCount',
    );
    _scaffoldKey.currentState.showSnackBar(
      SnackBar(
        backgroundColor: Colors.white,
        content: Image.memory(
          bodyBytes,
          fit: BoxFit.fill,
        ),
      ),
    );
  }
}``

如果以上代码或任何地方需要任何更改,请建议我

Flutter默认支持打开图片,打开pdf需要使用pdf插件(依赖)。 https://codecarbon.com/top-pdf-widgets-for-flutter/ 完成这个 link,我认为 7。 pdf_viewer_plugin 适合这个应用。

Dependencies:
flutter_full_pdf_viewer: ^1.0.6 # MIT  Licence

创建一个新文件 display-pdf.dart 并在其中添加以下代码。

import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';

class DisplayPDF extends StatelessWidget {
  final String pdfPath;
  final String title;

  DisplayPDF(this.pdfPath, this.title);

  @override
  Widget build(BuildContext context) {
    return PDFViewerScaffold(
        appBar: AppBar(
          title: Text(title),
          centerTitle: true,
        ),
        path: pdfPath);
  }
}

创建一个文件 pdf-utilities.dart 并在其中添加以下代码。


import 'dart:async';
import 'dart:typed_data';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

class PdfUtilities {
  final String pdfPath;
  final BuildContext context;

  PdfUtilities(this.pdfPath, this.context);

  Future<String> prepareTestPdf() async {
    final ByteData bytes = await DefaultAssetBundle.of(context).load(pdfPath);
    final Uint8List list = bytes.buffer.asUint8List();

    final tempDir = await getTemporaryDirectory();
    final tempDocumentPath = '${tempDir.path}/$pdfPath';

    final file = await File(tempDocumentPath).create(recursive: true);
    file.writeAsBytesSync(list);
    return tempDocumentPath;
  }
}

Future<void> downloadFile(StorageReference ref) async 替换为此代码:

Future<void> downloadFile(StorageReference ref) async {
  final String url = await ref.getDownloadURL();
  final http.Response downloadFile = await http.get(url);
  final Directory systemTempDir = Directory.systemTemp;
  final File tempFile = File('${systemTempDir.path}/tmp.pdf');
  if (tempFile.existsSync()) {
    await tempFile.delete();
  }
  await tempFile.create();
  final StorageFileDownloadTask task = ref.writeToFile(tempFile);
  final int byteCount = (await task.future).totalByteCount;
  var bodyBytes = downloadFile.bodyBytes;
  final String name = await ref.getName();  
  final String path = await ref.getPath();
  print(
    'Success!\nDownloaded $name \nUrl: $url'
        '\npath: $path \nBytes Count :: $byteCount',
  );
  final String title = 'Displaying PDF';
  PdfUtilities pdf = new PdfUtilities(path, context);
  pdf.prepareTestPdf().then(writeCounter(await bodyBytes),
        (path) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => DisplayPDF(path, title),
        ),
      );
    },
  );
}
Future<File> writeCounter(Uint8List stream) async {
  final file = await _localFile;

  // Write the file
  return file.writeAsBytes(stream);
}