无法在表单上呈现数据 [英] Not able to render data on form

查看:32
本文介绍了无法在表单上呈现数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下功能从Firebase提取数据,而我正在使用这些数据来预先填写表单.但是,尽管我成功获取了数据,但表单未预先填写.我假设它与异步和等待逻辑有关,但是我不确定问题到底是什么.感谢您的投入.

The following function fetches data from Firebase and I am using the data to pre-fill a form. However, the form is not pre-filled although I am successfully fetching data. I am assuming it has something to do with async and await logic but I am not sure what exactly the issue is. Will appreciate your inputs.

功能

Future<Address> fetchAddress() async { 
    final url = 'https://m&m.firebaseio.com/address/$userId.json?auth=$authToken';
    try {
      final response = await http.get(url);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;

       if (extractedData == null || extractedData['error'] != null) {
      return null;
    }
      //I can see the data in the logs
      print('Exctracted data is ' + extractedData.toString());
    
     // print(extractedData.)
      var address = Address(
        name: extractedData['name'], 
        details:extractedData['details'], 
        phoneNumber: extractedData['phoneNumber'], 
        alternatePhone: extractedData['alternatePhone']
      )  ; 
     
       return address; 
    } catch (error) {
      print('Inside error'); 
      print(error); 
      throw (error);
    }
  } 
 

address_screen.dart 文件中,我试图在didChangeDependecies函数中设置数据,如下所示:

In the address_screen.dart file, I am trying to set the data inside didChangeDependecies function as seen below:

 @override
  void didChangeDependencies() async{
    if (_isInit) {
       setState(() {
      _isLoading = true;
    });
      await Provider.of<Addresses>(context, listen: true).fetchAddress().
      then(
        (value){ 
          print('inside value'); 
          print(value == null); 
          if(value != null) 
            _editedAddress = value; 
        }
        );
        _initValues = {
          'name': _editedAddress.name,
          'details': _editedAddress.details,
          'alternatePhone': _editedAddress.alternatePhone,
        };
    }
     setState(() {
      _isLoading = false;
    });
    _isInit = false;
    super.didChangeDependencies();
  }

正如我提到的,当我记录函数结果时,我看到了数据库中的值.但是该表格并未预先填写.

As I mentioned, when I log my function results, I see the values from the DB. But the form does not get pre-filled.

这是我设置表单的方式(显示表单的一部分以避免代码长度)

Here's how I am setting the form (Showing part of form to avoid code length)

child: Form(
                key: _form,
                child: ListView(
                  children: <Widget>[
                      TextFormField(
                      initialValue: _initValues['name'],
                      decoration: InputDecoration(labelText: 'Contact name'),
                      textInputAction: TextInputAction.next,
                      onFieldSubmitted: (_) {
                        FocusScope.of(context).requestFocus(_nameFocusNode);
                      },
                      validator: (value) {
                        if (value.isEmpty) {
                          return 'Please provide a name.';
                        }
                        return null;
                      },
                      onSaved: (value) {
                        _editedAddress = Address(
                            name: value,
                            details: _editedAddress.details,
                            alternatePhone: _editedAddress.alternatePhone,
                            );
                      },
                    ),

推荐答案

好,所以这里的问题是您使用的是 initialValue:属性,但问题在于它仅在第一次调用构建.

Ok so the problem here is that you are using initialValue:property but the problem with that is it is called only 1st time during the build.

尝试使用 controller:属性,它需要一个 TextEditingController ,并且您可以在 didChangeDependencies _controller 的值>

Try using controller: property it takes an TextEditingController and you can set the value of your _controller in didChangeDependencies

在此处详细了解textEditingController及其用法文本编辑控制器

learn more about textEditingController and there usage over here text Editing controller

如果您在理解用法方面有困难,请在评论中让我知道

also if you find difficulty understanding the use here let me know in comments

这篇关于无法在表单上呈现数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆