Ujjwal Raijada
Ujjwal Raijada

Reputation: 975

Flutter - How to get value in TextFormField using Navigator

Container( padding: 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'],
      ),
    ],
  ),
);

When I am printing the value, it is give right result. But I am not able to get the result on TextFormField.

Upvotes: 0

Views: 133

Answers (1)

Lapa Ny Aina Tanjona
Lapa Ny Aina Tanjona

Reputation: 1268

Try the code below :

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']),
          ),
        ],
      ),

Upvotes: 1

Related Questions