Home > other >  How can i pass string data from one file to the other flutter
How can i pass string data from one file to the other flutter

Time:11-17

I want to pass the data of this string: String pathPDF = "assets/borst-oei.pdf"; to an other dart file named arbo.dart

The problem is that is don't know how I can do this with this: GestureDetector( onTap: () => Navigator.of(context).push(PageTransition( child: PDFScreen(), type: PageTransitionType.fade)),

i can't do this: GestureDetector( onTap: () => Navigator.of(context).push(PageTransition( child: PDFScreen(pathPDF: ''),type: PageTransitionType.fade)), because it isn't defined then

How can I fix this?

CodePudding user response:

What isn't "defined then" ? The parameter to the PDFScreen or the String pathPDF itself?

Just add the parameter to the constructor of the widget (if it is StatefulWidget that doesn't matter).

class PDFScreen extends StatelessWidget {
  const PDFScreen({Key? key, required this.pathPdf}) : super(key: key);
  final String pathPdf;
  ...
}

Then use it as:

GestureDetector(
  onTap: () => Navigator.of(context).push(
    PageTransition(
      child: PDFScreen(pathPDF: pathPDF),
      type: PageTransitionType.fade),
  ..
)

CodePudding user response:

Navigation from here with data

   GestureDetector(
                      onTap: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: ((context) => PDFScreen(
                                    pathPDF: pathPDF))));
                      },
                      );

Get data in another file

class PDFScreen extends StatelessWidget {
final String pathPdf;
  const PDFScreen({Key? key, required this.pathPdf}) : super(key: key);
  
  //your rest of the code
}
  • Related