简体   繁体   English

无效的双颤

[英]Invalid double-flutter

i Make a Form and I need to validate and save it when pressed.我制作一个表格,我需要在按下时验证并保存它。 so when i Press the Save Button i got this error: ════════ Exception caught by gesture ═══════════════════════════════════════════ The following FormatException was thrown while handling a gesture: Invalid double i tried double.tryParse() instead of double.parse() also i try to put in quotation.所以当我按下保存按钮时,我收到了这个错误:════════ 手势捕获的异常══════════════════════════ ═════════════════ 处理手势时抛出以下 FormatException: Invalid double 我尝试了 double.tryParse() 而不是 double.parse() 我也尝试引用.

i will mention where i got this error in the code below and Thank You for any help.我会在下面的代码中提到我在哪里得到这个错误,谢谢你的帮助。

class EditProductScreen extends StatefulWidget {

const EditProductScreen({Key? key}) : super(key: key);

@override
State<EditProductScreen> createState() => _EditProductScreenState();
}


class _EditProductScreenState extends State<EditProductScreen> {

final _form = GlobalKey<FormState>();
var product = Product(
id: '',
title: '',
price: 0.0,
imageUrl: '',
description: '',
);

 void _saveForm() {
_form.currentState?.validate();
_form.currentState?.save();
}

@override
Widget build(BuildContext context) {
 return Scaffold(
  appBar: AppBar(
    title: const Text('Edit/Add Products'),
    actions: [
      IconButton(
        onPressed: _saveForm,
        icon: const Icon(Icons.save),
      ),
    ],
  ),
  body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Form(
      key: _form,
      child: ListView(
        children: [
         
          TextFormField(
            decoration: const InputDecoration(
              hintText: 'Price',
              labelText: 'Price',
            ),
            textInputAction: TextInputAction.next,
            keyboardType: TextInputType.number,

Here Is The Error这是错误

            onSaved: (value) {
              product = Product(
                id: '',
                title: product.title,
                price:double.parse(value!),
                imageUrl: product.imageUrl,
                description: product.description,
              );
            },
            validator: (value) {
              if (value!.isEmpty) {
                return 'PLease insert a price';
              }

              if (double.tryParse(value)! <= 0.0) {
                return 'Please insert a value great than 0';
              }
              if (double.tryParse(value) == null) {
                return 'Please enter a Valid number';
              } else {
                return null;
              }
            },
          ),
         
        

You can try你可以试试

price:(double.tryParse(value) ?? 0),

If the double.tryparse returns a null value then 0 is assigned如果 double.tryparse 返回 null 值,则分配 0

And in validator在验证器中

         if ((double.tryParse(value) ?? 0) <= 0.0) {
                return 'Please insert a value great than 0';
              }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM