pass a value from contructon to state flutter code example

Example 1: send params to stateful widget

class Screen1 extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return Center(
      child: Screen2(userId: "89d6db7e-7469-4888-9fac-38c300628ff1") // sends the value
    );
  }
}

class Screen2 extends StatefulWidget {
  final String userId; // receives the value

  Screen2({ Key key, this.userId }): super(key: key);

  
  _Screen2State createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  
  Widget build(BuildContext context) {
    return Text(widget.userId); // uses the value
  }
}

Example 2: get data from args in flutter

var nextPageData = {foo:'bar'};

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => 
     MyNextPage(myData: nextPageData))
 );

Tags:

Dart Example