Yannick
Yannick

Reputation: 25

Is there an easy way to get the initialvalue in a textformfield in a variable?

I just started with flutter and I'm making a profile page and i'm making a request. I'm trying to create an edit profile page so whenever i go there the value's of the textformfield are allready filled in. At first i thought it would be good to use a placeholder for the initialvalue. But i was thinking and this is not good for the user experience. I'd rather have that the textformfields are filled in with the correct data. So if the user wants to update his information the fields are filled in. I'm making a http post request and i get the correct value's back. But the weird thing what i don't get is why i can load the variables in the labeltext But i can't load them in the initialvalue...

I have tried to look for a solution and i have tried a futurebuilder but i couldn't get that to work. I hope someone can help me with this.

   Expanded(
                                child: Container(
                                  child: new TextFormField(
                                    onSaved: (input) => _voornaam = input,
                                    initialValue: '$voornaam',
                                    decoration: InputDecoration(
                                        labelText: '$voornaam'),
                                    style: TextStyle(
                                        fontSize: 16.0,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),
                                flex: 2,
                              ),        

I expect to get the value of $voornaam in my textformfield. But actually when i load it it just comes back with null. My http request is working because the value is putted in the labeltext. If someone

Upvotes: 1

Views: 5412

Answers (2)

Keerti Purswani
Keerti Purswani

Reputation: 5361

Add controller to your TextFormField and in initState you can initialise it to the initial value - _controller = TextEditingController(text: $voornaam);.

Upvotes: 1

Taym95
Taym95

Reputation: 2510

You have a typo, this code should work:


void main() => runApp(MaterialApp(
      home: App(),
    ));

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  String _voornaam = 'Helo';

  @override
  void initState() {
    super.initState();
    // set you _voornaam value
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('text'),
      ),
      body: Container(
        child: new TextFormField(
          onSaved: (input) => setState(() {
                _voornaam = input;
              }),
          initialValue: '$_voornaam',
          decoration: InputDecoration(labelText: '$_voornaam'),
          style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }
}

Upvotes: 0

Related Questions