Giter Club home page Giter Club logo

Comments (6)

danvick avatar danvick commented on May 12, 2024

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

from flutter_form_builder.

DinoSane avatar DinoSane commented on May 12, 2024

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

git clone [email protected]:dinosane/form_builder.git

thanks in advance.

from flutter_form_builder.

danvick avatar danvick commented on May 12, 2024

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

from flutter_form_builder.

DinoSane avatar DinoSane commented on May 12, 2024

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.

from flutter_form_builder.

DinoSane avatar DinoSane commented on May 12, 2024

I forgot to say: great job! :-)

from flutter_form_builder.

danvick avatar danvick commented on May 12, 2024

You're welcome @DinoSane. Thank you.

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

from flutter_form_builder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.