Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

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

Open
Appvala opened this issue May 30, 2021 · 14 comments

Comments

@Appvala
Copy link

Appvala commented May 30, 2021

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
Copy link

Same for me, using latest flutter version 2.2.1

@gorkemg0
Copy link

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

@himalaya-08
Copy link

same for me

1 similar comment
@quocviet1996
Copy link

quocviet1996 commented Jun 1, 2021

same for me

@hkhars
Copy link

hkhars commented Jun 1, 2021

I am facing the same issue with flutter v2.2.1

@iamvikashh
Copy link

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
Copy link

gorkemg0 commented Jun 1, 2021

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
Copy link

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
Copy link

AdiD253 commented Jun 1, 2021

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
Copy link

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
Copy link

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- #51.

@rsanjuan87
Copy link

rsanjuan87 commented Jun 10, 2021

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) <asynchronous suspension> #1 _PDFPageState._initPdf (package:catalogo/pdfview.dart:31:17) <asynchronous suspension>

Even with the sample project

@rsanjuan87
Copy link

not working on MacOS desktop app
Android and iOS working well

@juscghwe
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests