Flutter:初始值未在FormFormBuilder中更新

问题描述

 _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个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(),);
  }
,

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

我用 reverpod with flutter form 下面是用 watch 粗略实现的相关代码

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,]));}
,

苦苦挣扎了半天终于找到了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,20,0),child: FormBuilder(
        key: commonForm,.....