Flutter : Future builder not updating form values

1.4k views Asked by At

Using Future builder, able to populate earlier saved values of form fields at the time of page loading. But when snapshot is null, it has to display empty form

This below code is working fine when form has some initials values

     Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            body: Container(
              height: MediaQuery.of(context).size.height,      
              child:FutureBuilder(
                future: _getPersonalInfoFormInitialValue(),
               builder: (context, snapshot) => snapshot.hasData ? buildFormBuilder(context, snapshot.data) : Center(child: Text('Loading .....')),
              ),
            ),
          ),
        );

  }

This code is working fine when snapshot.data is null, its displaying empty form as expected

FutureBuilder(
            future: _getPersonalInfoFormInitialValue(),
           builder: (context, snapshot) => buildFormBuilder(context, snapshot.data) ,
          ),

So I have combined both to get the form working for all cases as,

Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Container(
          height: MediaQuery.of(context).size.height,      
          child:FutureBuilder(
            future: _getPersonalInfoFormInitialValue(),
           builder: (context, snapshot) => snapshot.hasData ? buildFormBuilder(context, snapshot.data) : buildFormBuilder(context, null) ,
          ),
        ),
      ),
    );
  }

buildFormBuilder method :

FormBuilder buildFormBuilder(BuildContext context, data) {
        return FormBuilder(
          key: _personalDetailFormKey,
          initialValue: data ?? {},
          autovalidate: true,
          child: Stack(
            children: <Widget>[
                 .........
                          _submitButton(() {
                            _submitPersonalDetailInfoForm();
                          }, 'Submit'), 
            ],
          ),
        );
  }

_getPersonalInfoFormInitialValue

_getPersonalInfoFormInitialValue() async {
    return _personalInfo!=null ? _personalInfo.toPersonalInfoMap() : null;
  }

So now, in both the cases, its displaying only empty fields of the form. eventhough snapshot.data has the values but its not populating into form fields. I have tried with Future.delayed(const Duration(seconds: 10), () {}); still facing issue.

Something is getting wrong somewhere, not able to identify, any help appreciated !! Thanks

1

There are 1 answers

1
Grisstusss On

Maybe because FutureBuilder is not a child:

FutureBuilder(
            future: _getPersonalInfoFormInitialValue(),
           builder: (context, snapshot) => buildFormBuilder(context, snapshot.data) ,
          ),

Maybe this works because Futurebuilder is not a child and in the next one it is:

child:FutureBuilder