2
 _updatePersonalFormScreen(String loginId) async {
    if (!DartUtility.isNullEmptyOrWhitespace(loginId)) {
      _personalInfo = await _service.getUserPersonalDetails(loginId);
      setState(() {
        if (_personalInfo != null) {
          if(!DartUtility.isNullEmptyList(_personalInfo.getContacts())){
            contactList = _personalInfo.getContacts();
          }     
          personalInfoMap = _personalInfo.toPersonalInfoMap();
        }
        print('personalInfo retrieved object ${_personalInfo.toString()}');   //1
      });
    }     
  }

formBuilder 小部件:

  FormBuilder buildFormBuilder(BuildContext context) {
    print('personalInfoMap $personalInfoMap');         //2
    return FormBuilder(
          key: _personalDetailFormKey,
          initialValue: personalInfoMap,               //3
          autovalidate: true,
          child: Stack(),
        );
  }

//第 1 行和第 2 行打印正确的值,但在第 3 行,初始值未分配给表单构建器文本框

'contactList' 正在正确填充,并且在同一块中填充 'personalInfoMap' 无法按预期正常工作,或者可能在第 3 行分配的值需要修改其他内容以使其工作

我也尝试过与 Future builder 合作,但没有运气。如果“contactList”工作正常并分配给表单值,那么为什么要在其他领域面临问题?:(

有人可以帮我解决这个问题,这里还需要做什么以及哪里出错了。

4

3 回答 3

1

经过4个5小时的奋斗,终于解决了,救星就是“未来的建设者”。这是解决方案,

与其在 build 方法中直接调用 FormBuilder,不如将其包装在 FutureBuilder 中

@override
  Widget build(BuildContext context) =>SafeArea(
        child: Scaffold(
          body: Container(
            height: MediaQuery.of(context).size.height,
            child: FutureBuilder(
              future: _getPersonalInfoFormInitialValue(),
              builder: (context, snapshot) => snapshot.hasData
                  ? buildFormBuilder(context, snapshot.data) // this provide returned data from _getPersonalInfoFormInitialValue()
                  : const SizedBox(),
            ),
          ),
        ),
      );

修改 formBuilder 小部件:

FormBuilder buildFormBuilder(BuildContext context, data) {
    print('datat ::::$data');
        return FormBuilder(
          key: _personalDetailFormKey,
          initialValue:data,               //assigned fetched data
          autovalidate: true,
          child: Stack(), 
      );
  }
于 2020-09-17T18:02:19.767 回答
0

似乎最初加载的值仍然无法更改,仍然_formKey保留在内存中。所以我们需要防止第一次用null初始化

我使用颤振形式的reverpod以下是使用手表粗略实现的相关代码

Widget build(
    BuildContext context,
    ScopedReader watch,
  ) {
    final loginId = context.read(selectedLoginId).state; // user id to check if there is a valid data
        final user = watch(selectedUser).data?.value; // getting user info we need both
       
        return Padding(
          padding: const EdgeInsets.all(32.0),
          child: Column(
            children: [
              (loginId != null && user == null)
                  ? CircularProgressIndicator()
                  : FormBuilder(
              key: _personalDetailFormKey,
              initialValue:user,               //assigned fetched data
              autovalidate: true,
              child: Stack(), 
          ),]));}
于 2021-05-26T05:14:36.847 回答
0

挣扎了好久终于找到form_builder更新问题的解决办法

class _CommonContentFormState extends State<CommonContentForm>
var commonForm = GlobalKey<FormBuilderState>();

@override
Widget build(BuildContext context) {
// Must create new GlobalKey before building form to update
// with new data from Provider...
    commonForm = GlobalKey<FormBuilderState>(); 
    formData = Provider.of<FormData>(context); 
    return SingleChildScrollView(
      padding: EdgeInsets.fromLTRB(0,0,20,0),
        child: FormBuilder(
        key: commonForm,
        .....
于 2021-08-02T11:46:01.903 回答