lohanidamodar / pdf_viewer

A flutter plugin for handling PDF files. Works on both Android & iOS
https://pub.dev/packages/advance_pdf_viewer
BSD 3-Clause "New" or "Revised" License
61 stars 144 forks source link

Just switched from flutter_plugin_pdf_viewer to advance_pdf_viewer, and facing 3 issues #50

Open Appvala opened 3 years ago

Appvala commented 3 years ago

Just switched from flutter_plugin_pdf_viewer to advance_pdf_viewer, and facing 3 issues while running the app. These are the 3 issues :

1) ../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart:64:14: Error: The class 'File' is abstract and can't be instantiated. file = File("${dir.path}/file.pdf");

2) ../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart:72:26: Error: Non-nullable variable 'file' must be assigned before it can be used. document._filePath = file.path;

3) ../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart:75:58: Error: Non-nullable variable 'file' must be assigned before it can be used. .invokeMethod('getNumberOfPages', {'filePath': file.path});

I am using this code:

` class _ChapterState extends State { bool _isLoading = true; PDFDocument document;

@override void initState() { super.initState(); loadDocument(); }

loadDocument() async { document = await PDFDocument.fromAsset(widget.category.chapter); setState(() => _isLoading = false); }

@override Widget build(BuildContext context) { return Scaffold( backgroundColor: Theme.of(context).scaffoldBackgroundColor, appBar: AppBar( centerTitle: false, backgroundColor: Theme.of(context).accentColor, title: Text( widget.category.name, ), ), body: Center( child: _isLoading ? Center( child: CircularProgressIndicator(), ) : PDFViewer(document: document), ), ); } } ` Kindly help.

EugenEistrach commented 3 years ago

Same for me, using latest flutter version 2.2.1

gorkemg0 commented 3 years ago

I am having the same problem. I am using flutter version 2.2.1

himalaya-08 commented 3 years ago

same for me

quocviet1996 commented 3 years ago

same for me

hkhars commented 3 years ago

I am facing the same issue with flutter v2.2.1

iamvikashh commented 3 years ago

I think the problem is due to the import file in the plugin itself. I don't know for sure but the File class object we are passing is from 'dart:io' but in the plugin, the File which it is expecting is abstract class File.

gorkemg0 commented 3 years ago

yes, I saw the problem with the file class too. I added 'as' while importing. My problem is not solved. there is still a problem.

kushalmahapatro commented 3 years ago

Came across this issue after upgrading to Flutter 2.2.1 and even the same persisted after downgrading it to 2.2.0.

Sol, that worked:

Edited the /flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart

import 'dart:io'; - > import 'dart:io' as io; : line 2

static Future fromAsset(String asset) async { io.File file; // Edited try { var dir = await getApplicationDocumentsDirectory(); file = io.File("${dir.path}/file.pdf"); // Edited var data = await rootBundle.load(asset); var bytes = data.buffer.asUint8List(); await file.writeAsBytes(bytes, flush: true); } catch (e) { throw Exception('Error parsing asset file!'); } PDFDocument document = PDFDocument(); document._filePath = file.path; try { var pageCount = await _channel .invokeMethod('getNumberOfPages', {'filePath': file.path}); document.count = document.count = int.parse(pageCount); } catch (e) { throw Exception('Error reading PDF!'); } return document; }

AdiD253 commented 3 years ago

Here's the working pubspec dep for a fork with the change above:

  advance_pdf_viewer:
    git:
      url: https://github.com/AdiD253/pdf_viewer.git
      ref: pdf-viewer-file-fix
himalaya-08 commented 3 years ago

Came across this issue after upgrading to Flutter 2.2.1 and even the same persisted after downgrading it to 2.2.0.

Sol, that worked:

Edited the /flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart

import 'dart:io'; - > import 'dart:io' as io; : line 2

static Future fromAsset(String asset) async { io.File file; // Edited try { var dir = await getApplicationDocumentsDirectory(); file = io.File("${dir.path}/file.pdf"); // Edited var data = await rootBundle.load(asset); var bytes = data.buffer.asUint8List(); await file.writeAsBytes(bytes, flush: true); } catch (e) { throw Exception('Error parsing asset file!'); } PDFDocument document = PDFDocument(); document._filePath = file.path; try { var pageCount = await _channel .invokeMethod('getNumberOfPages', {'filePath': file.path}); document.count = document.count = int.parse(pageCount); } catch (e) { throw Exception('Error reading PDF!'); } return document; }

This has to be fixed asap.

kushalmahapatro commented 3 years ago

Came across this issue after upgrading to Flutter 2.2.1 and even the same persisted after downgrading it to 2.2.0. Sol, that worked: Edited the /flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart import 'dart:io'; - > import 'dart:io' as io; : line 2 static Future fromAsset(String asset) async { io.File file; // Edited try { var dir = await getApplicationDocumentsDirectory(); file = io.File("${dir.path}/file.pdf"); // Edited var data = await rootBundle.load(asset); var bytes = data.buffer.asUint8List(); await file.writeAsBytes(bytes, flush: true); } catch (e) { throw Exception('Error parsing asset file!'); } PDFDocument document = PDFDocument(); document._filePath = file.path; try { var pageCount = await _channel .invokeMethod('getNumberOfPages', {'filePath': file.path}); document.count = document.count = int.parse(pageCount); } catch (e) { throw Exception('Error reading PDF!'); } return document; }

This has to be fixed asap.

Submitted a PR containing the fix- https://github.com/lohanidamodar/pdf_viewer/pull/51.

rsanjuan87 commented 3 years ago

Came across this issue after upgrading to Flutter 2.2.1 and even the same persisted after downgrading it to 2.2.0.

Sol, that worked:

Edited the /flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart

import 'dart:io'; - > import 'dart:io' as io; : line 2

static Future fromAsset(String asset) async { io.File file; // Edited try { var dir = await getApplicationDocumentsDirectory(); file = io.File("${dir.path}/file.pdf"); // Edited var data = await rootBundle.load(asset); var bytes = data.buffer.asUint8List(); await file.writeAsBytes(bytes, flush: true); } catch (e) { throw Exception('Error parsing asset file!'); } PDFDocument document = PDFDocument(); document._filePath = file.path; try { var pageCount = await _channel .invokeMethod('getNumberOfPages', {'filePath': file.path}); document.count = document.count = int.parse(pageCount); } catch (e) { throw Exception('Error reading PDF!'); } return document; }

This solution doesn't work for me with Flutter 2.2.1

`[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Exception: Error reading PDF!

0 PDFDocument.fromAsset (package:advance_pdf_viewer/src/document.dart:78:7)

#1 _PDFPageState._initPdf (package:catalogo/pdfview.dart:31:17) ` Even with the sample project
rsanjuan87 commented 3 years ago

not working on MacOS desktop app Android and iOS working well

juscghwe commented 1 year ago

Came across this issue after upgrading to Flutter 2.2.1 and even the same persisted after downgrading it to 2.2.0.

Sol, that worked:

Edited the /flutter/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-2.0.0/lib/src/document.dart

import 'dart:io'; - > import 'dart:io' as io; : line 2

static Future fromAsset(String asset) async { io.File file; // Edited try { var dir = await getApplicationDocumentsDirectory(); file = io.File("${dir.path}/file.pdf"); // Edited var data = await rootBundle.load(asset); var bytes = data.buffer.asUint8List(); await file.writeAsBytes(bytes, flush: true); } catch (e) { throw Exception('Error parsing asset file!'); } PDFDocument document = PDFDocument(); document._filePath = file.path; try { var pageCount = await _channel .invokeMethod('getNumberOfPages', {'filePath': file.path}); document.count = document.count = int.parse(pageCount); } catch (e) { throw Exception('Error reading PDF!'); } return document; }

Requires another change to Line 22: static Future<PDFDocument> fromFile(File file) aync to static Future<PDFDocument> fromFile(io.File file) aync

Sadly still not working with Exception Error parsing asset file! with example code provided by the package.