Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set the initial values of the fields #3

Closed
DinoSane opened this issue Nov 13, 2018 · 6 comments
Closed

Unable to set the initial values of the fields #3

DinoSane opened this issue Nov 13, 2018 · 6 comments

Comments

@DinoSane
Copy link

I've tried the simplest FormBuilder to read and write SharedPreferences.

TextFormField(initialValue: _name ) shows as it should

FormBuilderInput.textField(
type: FormBuilderInput.TYPE_TEXT,
attribute: "name",
value: _name,
label: "Name:",
)

Does not!

@danvick
Copy link
Collaborator

danvick commented Nov 13, 2018

Hello @DinoSane,
I'm unable to reproduce your issue, could you kindly elaborate on the issue, more code could be helpful.

@DinoSane
Copy link
Author

I created a simple project to ilustrate the problem
can you clone it?

git clone git@bitbucket.org:dinosane/form_builder.git

thanks in advance.

@danvick
Copy link
Collaborator

danvick commented Nov 14, 2018

Please note that your string is fetched from SharedPreferences after the form is already built since the fetching is asyncronous. See code below and the corresponding console output:

`class _FailingPageState extends State {
String _aStringValue = '';

@OverRide
void initState() {
_loadVals();
super.initState();
}

//Loading counter value on start
_loadVals() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('aStringValue', 'My string value');
_aStringValue = prefs.getString('aStringValue');
print("After fetch '$_aStringValue'");
}

@OverRide
Widget build(BuildContext context) {
print("During build '$_aStringValue'");
return SharedScaffold(
titulo: 'Failing',
body: Padding(
padding: EdgeInsets.all(20.0),
child: Padding(
padding: EdgeInsets.all(20.0),
child: FormBuilder(
context,
autovalidate: true,
controls: [
FormBuilderInput.textField(
type: FormBuilderInput.TYPE_TEXT,
attribute: "aStringValue",
value: _aStringValue,
label: "Any Value:",
)
],
onChanged: () async {},
onSubmit: (formValue) async {
if (formValue != null) {
print(formValue);
final prefs = await SharedPreferences.getInstance();
prefs.setString('aStringValue', formValue['aStringValue']);
} else {
print("Form invalid");
}
},
),
),
),
);
}
}`

Console:
Performing hot restart... Syncing files to device Android SDK built for x86... Restarted application in 1,699ms. I/flutter ( 4934): During build '' I/flutter ( 4934): After fetch 'My string value'

I'd advise declaring the form within a FutureBuilder or a similar implementation

@DinoSane
Copy link
Author

You are correct!

Using a FutureBuilder solve the problem.

I though setting the value inside a setState was suffice, but as you pointed, it doesn't.
Thanks.

@DinoSane
Copy link
Author

I forgot to say: great job! :-)

@danvick
Copy link
Collaborator

danvick commented Nov 14, 2018

You're welcome @DinoSane. Thank you.

Also note that you're not really required to use setState() when setting a value within initState()

deandreamatias pushed a commit that referenced this issue Jun 23, 2024
fix: respect initial value on field re-creation, from formbuilder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants