I want to load other screen properly on my flutter app

Viewed 130

In my AppBar menu I tried to put a button with this function (for change the screen):

        onPressed: (){ 
            Navigator.push(
            context,
             MaterialPageRoute(builder: (context) => Read_Pdf()),
          );
        }

But always appears "... => Read_Pdf())," with error cause "2 positional argument(s) expected, but 0 found. Try adding the missing arguments."

This is the content of the file.dart which contains the Read_Pdf Class:

import 'dart:io';
import 'dart:typed_data';

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

const String _documentPath = 'PDFs/manual_demo.pdf';

// ignore: camel_case_types
class Read_Pdf extends StatelessWidget {

  final BuildContext context;
  String pdfPath;
  Read_Pdf(this.context, this.pdfPath);

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

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

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

    return tempDocumentPath;
  }


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

I know that there is a conflict with the pdfPath variable, but I don´t know how to solve it.

3 Answers

It happens because you have not passed the arguments to the constructor of Read_Pdf()

Read_Pdf(this.context, this.pdfPath);

It needs context and the pdfPath, you can do it like this

onPressed: (){ 
        Navigator.push(
        context,
         MaterialPageRoute(builder: (context) => Read_Pdf(context, pdfPath)),
      );
    }

That it'll do it.

But in your implementation:

I can see you have a demo pdf in Read_Pdf

Change your constructor to

Read_Pdf({this.context, this.pdfPath = _documentPath});

What this.pdfPath = _documentPath does is it assigns _documentPath to pdfPath if it's not provided when Read_Pdf is instantiated.

So now the onPressed function will change to

onPressed: (){ 
        Navigator.push(
        context,
         MaterialPageRoute(builder: (context) => Read_Pdf(context: context)),
      );
    }

If you don't need this variable to forward to class Read_pdf from other class, you must remove constructor: Read_Pdf(this.context, this.pdfPath); If you remove this one line, error disappears.

If You need this variables, you must run class with arguments:

MaterialPageRoute(builder: (context) => Read_Pdf(context: context, pdfPath: pdfPath)),

it needs context and pdf path() try the following code :

onpressesd(){
Navigator.push(context,MaterialPageRoute(builder :(context) => Read_pdf(context: context)))
}
Related