Home > Back-end >  How to acces parameter value from constructor in flutter?
How to acces parameter value from constructor in flutter?

Time:05-01

I'm having trouble with accessing the value of a parameter from a constructor in my code. I access to this page from another one where I get the value of the parameter:

final route = MaterialPageRoute(
                          builder: (context) => AnadirJugadores(idPagina: respuesta['id'],));
                      Navigator.push(context, route);

This is the code of AnadirJugadores:

class AnadirJugadores extends StatefulWidget {
    final String idPagina;
  AnadirJugadores({required this.idPagina });
  
  String cogerID() {
    return this.idPagina;
  }

  @override
  State<AnadirJugadores> createState() => _AnadirJugadoresState();
}

class _AnadirJugadoresState extends State<AnadirJugadores> {
  
  @override
  Widget build(BuildContext context) {
      
    .... more code
    ElevatedButton(
                      child: Text(idPartida), // this is the line of the error
                      onPressed: () {
                        final data = ClipboardData(text: '25342756374');
                        Clipboard.setData(data);
                      },
                    ),
I'm trying to access the value of idPagina. How could I do that? Thanks in advance.

CodePudding user response:

When using stateful widgets you need to use widget to access the parameters.

child: Text(widget.idPartida),

CodePudding user response:

In StatefulWidget your constructor exists in upper class (it's not in State class), so to access data in this constructor in your state class you should do this:

ElevatedButton(child: Text(widget.idPartida), ...),

  • Related