San Juan
San Juan

Reputation: 107

Error: Exception: Error parsing asset file! Flutter App with advance_pdf_viewer

I'm trying to develop an app in flutter to display a pdf file but I am not managing to load the file from the assets

This is my code:

class PDFPages extends StatefulWidget {
  @override
  _PDFPagevvvState createState() => _PDFPagevvvState();
}

class _PDFPagesState extends State<PDFPagevvv> {
  PDFDocument _doc;
  bool _loading;

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

  _initPdf() async {
    setState(() {
      _loading = true;
    });
    final doc = await PDFDocument.fromAsset(Assets.assetsSample);
    setState(() {
      _doc = doc;
      _loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test"),
      ),
      body: _loading
          ? Center(
              child: CircularProgressIndicator(),
            )
          : PDFViewer(
              document: _doc,
              indicatorBackground: Colors.red,
              // showIndicator: false,
              // showPicker: false,
            ),
    );
  }
}

In fact it's almost the same as the sample

This is the error:

[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:pdfview.dart:31:17) <asynchronous suspension>

I have tried to solve like https://github.com/lohanidamodar/pdf_viewer/issues/50#issuecomment-852104442 but It doesn't work for me

Upvotes: 0

Views: 1214

Answers (1)

David
David

Reputation: 366

This is a conflict of File in dart:io

Edit the document.dart file (part of advance_pdf_viewer) and change...

import 'dart:io';

to

import 'dart:io' as io;

then in fromAsset, change...

File file;

to

io.File file;

and anywhere else you find File

Upvotes: 1

Related Questions