Home > Mobile >  Flutter Pass value from one screen to another through controller
Flutter Pass value from one screen to another through controller

Time:02-24

TextFormField(initialValue: widget.value) is one way but in this case controller missed out

needs to pass value through controller

TextEditingController _text1=TextEditingController(text: widget.value) ; this shows error

can anyone help.

CodePudding user response:

You can initialize TextEditingController in initState. Try like this:

class Home extends StatefulWidget {
  final String value;
  const Home({Key? key, required this.value}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextEditingController? _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.value);
  }

  @override
  Widget build(BuildContext context) {
    return ...
  }
}

CodePudding user response:

Try this in your second screen.

@override
void initState() {

_text1.value = widget.value; 

  super.initState();
}
  • Related