skip to Main Content

I am new to the flutter and trying to understand it better.

  FormFieldText(
                  label: "First Name",
                  initialValue:firstName,
                  validator: (value) {
                    if (value.isEmpty) return "Name must be provided";
                    return null;
                  },
                  onChanged: ((value) {
                    setState(() {
                      member?.firstName = value;
                    });
                  }),
                ),

3

Answers


  1. Using the function setState forces the ui to rebuild which will show the changes to screen but not using it does nothing

    Login or Signup to reply.
  2. If u want to change something in your screen with that value, you should put it in set state then user can see every change, it is usable in search bar, every word you type will search data base and show result. But if you Don’t use set state in on change, UI doesn’t show any thing until u refresh page. If you don’t want to show every changes in typing, you can use controller and save the value with a button in the last.

    Login or Signup to reply.
  3. You can assign the value directly without setState(), but then you don’t really require to use the onChanged property at all if you are not willing to display a text or run/callback a function while the user is typing.

    This property is more used if you need to build a feature that will constantly listen to a user’s input. It will callback a function everytime a user inputs a new character in the formfield.

    In your example above, your function is to delete the variable member?.firstName and replace it with a new value everytime a user input something. This is useful to show a preview for the user as he is typing for example. You would just need a Text widget displaying somewhere in the screen like Text (member?.firstName.toString). It will visually display the same letters as what the user inputs 1 by 1.

    The onChanged property is useful for functions like search, where you show suggestions while the user is typing. Another example is to display the number of characters like 200/2000 words, and let the user know how much characters he has left before reaching max.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search