Flutter - 如何使用 Navigator 在 TextFormField 中获取值

Flutter - How to get value in TextFormField using Navigator

容器( 填充:EdgeInsets.all(15),

  child: Column(
    children: [
      TextFormField(
        onTap: () =>
            Navigator.of(context).pushNamed(AirportSearchScreen.id).then(
                  (value) 
              {
            setState(() {
              _initValues['departureCity'] = value;
              print(_initValues['departureCity']);
            });
          },
                ),
        decoration: InputDecoration(
          labelText: 'Departure City',
        ),
        initialValue: _initValues['departureCity'],
      ),
    ],
  ),
);

当我打印值时,它给出了正确的结果。但是我无法在 TextFormField 上获得结果。

试试下面的代码:

child: Column(
        children: [
          TextFormField(
            onTap: () =>
            Navigator.of(context).pushNamed(AirportSearchScreen.id).then(
                  (value) 
              {
               setState(() {
                    _initValues['departureCity'] = value;
                    print(_initValues['departureCity']);
                  });
                },
                ),
            decoration: InputDecoration(
              labelText: 'Departure City',
            ),
            controller: TextEditingController(text: _initValues['departureCity']),
          ),
        ],
      ),