pass an object to a widget flutter code example
Example 1: pass string to a provider flutter
class SecondPage extends StatelessWidget { final Data data; SecondPage({this.data}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(‘Constructor — second page’), ), body: Container( padding: EdgeInsets.all(12.0), alignment: Alignment.center, child: Column( children: [ Container( height: 54.0, padding: EdgeInsets.all(12.0), child: Text(‘Data passed to this page:’, style: TextStyle(fontWeight: FontWeight.w700))), Text(‘Text: ${data.text}’), Text(‘Counter: ${data.counter}’), Text(‘Date: ${data.dateTime}’), ], ), ), ); }}
Example 2: pass string to a provider flutter
_secondPage(BuildContext context, Widget page) async { final dataFromSecondPage = await Navigator.push( context, MaterialPageRoute(builder: (context) => page), ) as Data; // Here we have the data from the second screen data.counter = dataFromSecondPage.counter; data.dateTime = dataFromSecondPage.dateTime; data.text = dataFromSecondPage.text;}