'How to retrieve a value from Widget

I'm very new to coding so i don't know too much, i'm sorry. I'm trying to get the value passed from screen 1 to screen 2, but the value is inside the widget from stateful. like that:

class Escrita extends StatefulWidget {


  List<Diario> _diarioupdate = <Diario>[];
  Escrita({Diario? teste});



  @override
  State<Escrita> createState() => _EscritaState();
}

and i want to retrieve the value teste that is passed from screen 1, how can i do that?



Solution 1:[1]

I believe you are asking how to access the _diarioUpdate inside your stateful widget. This can be accomplished removing the underscore(_) from the _diarioupdate so that it won't be private member and then calling it inside the statefulwidget as widget.diarioupdate.

Solution 2:[2]

i found the answer. Thank you so much for helping. I solved like this:

Escrita({Diario? teste}){
this.teste};


Solution 3:[3]

in _EscritaState you can can call widget._diarioupdate.

for example you have HomeWidget as StatefulWidget that needs a title

class HomePage extends StatefulWidget {
  final String title;

  const HomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

you can access the title value using widget field in the State class


class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title), // <----
      ),
      body: Container()
    );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karunjith M
Solution 2 Wagner Tiburcio
Solution 3