我怎样才能方便地将Response.errors绑定到TextFormField小部件中的errorText标签(输入修饰)

时间:2019-09-11 14:09:22

标签: api http flutter

变量

Map <String, dynamic> customerFormErrors;

# Widget
TextFormField(
 keyboardType: TextInputType.emailAddress,
 decoration: InputDecoration(
     labelText: 'Customer Email',
     errorText: '${customerFormErrors.email}',
     border: OutlineInputBorder(),
     fillColor: mainColor,
     prefixIcon: Icon(Icons.email),
    ),
onSaved: (value) {
     this.customer.email = value;
},
),
);

# Submit Function
  _submitForm(Customer customer) async {


      Create a Customer 
    */

    Response response = await createCustomer(this.customer);
    if (response.statusCode == 201 || response.statusCode == 200) {

      // Customer has been added

      Scaffold.of(context).showSnackBar(
          SnackBar(backgroundColor: mainColor, content: Text('Added')));
    } else {

      // Notify him with errors

      setState(() {
        customerFormErrors = jsonDecode(response.body);
      });
    }
  }

我正在发布到API,然后将错误响应正文附加到变量。 有没有办法将该变量绑定到TextFormField的errorText。

有点像angular,我们在其中使用类型安全运算符,例如以下{{error?.name}}吗?

我在这里发布了它:https://github.com/flutter/flutter/issues/40254

0 个答案:

没有答案