简体   繁体   中英

pass one variables value to another variable in flutter

In my flutter app I created variables var from, to; and pass those with their values to the next page. which I used it with the widget.from & widget.to . but how can i pass this widget.from & widget.to variables to var newFrom, newTo; these variables?

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var from = "Five", to = "Ten";
    return SecondPage(from: from, to: to);
  }
}



class SecondPage extends StatefulWidget {
  final from, to;
  const SecondPage({Key key, this.from, this.to}) : super(key: key);
  @override
  _SecondPageState createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  var newFrom = "", newTo = "";

  @override
  void initState() {
    setState(() {
      newFrom = widget.from;
      newTo = widget.to;
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("$newFrom $newTo"),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM