gpt4 book ai didi

validation - 在Flutter中无法在验证器中获得适当的值

转载 作者:行者123 更新时间:2023-12-03 04:18:36 25 4
gpt4 key购买 nike

重现步骤

  • Here is the dartpad link转到它。
  • 在字段中输入任何值以计算答案。
  • 将0添加到任何字段
  • 的开头

    预期结果:验证程序应返回0.xyz
    实际结果:验证程序返回0xyz
    解决方法:使用onSaved的解决了此问题
    谁能告诉我为什么会这样或它是否会在Flutter中出错,那么我将在Flutter Repo中发布一个新问题
    issue
    我正在打印来自验证程序的值,并在将字符串值转换为int的onChanged中进行打印。

    最佳答案

    编辑
    当输入23,然后输入惰性0.时,此行amount = int.parse(val);执行并停止,然后单击“计算”按钮,因此数量为23,而valueruntimeTypeString 023

    TextFormField(
    keyboardType: TextInputType.number,
    validator: (value) {
    print("amount $amount");
    print(" ${value.runtimeType}");
    print("validator amount value $value ");
    if (value.isEmpty) {
    return "Enter some amount";
    } else if (double.parse(value).toInt() <= 0) {
    return "Amount should be greater than 0";
    }
    return null;
    },
    onChanged: (val){
    print("val1 $val");
    amount = int.parse(val);
    print("amount $amount");
    }
    您可以在下面复制粘贴运行完整代码 validator不返回 0.xyz的原因
    因为 onChangedvalidator之前执行
    因此, validator通过 int.parse(value)接收截断的值
    如果删除 amount = int.parse(value);,验证程序将获得正确的值,但是
    此条件 if (int.parse(value) <= 0)将获得 Invalid radix-10 number您需要更改为 if (double.parse(value).toInt() <= 0)工作演示
    enter image description here
    完整的代码
    import 'package:flutter/material.dart';

    void main() {
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
    primarySwatch: Colors.purple,
    buttonTheme: ButtonThemeData(
    buttonColor: Colors.purple.shade400,
    ),
    visualDensity: VisualDensity.adaptivePlatformDensity,
    ),
    home: BillSpitApp(),
    );
    }
    }

    class BillSpitApp extends StatefulWidget {
    BillSpitApp({Key key}) : super(key: key);

    @override
    _BillSpitAppState createState() => _BillSpitAppState();
    }

    class _BillSpitAppState extends State<BillSpitApp> {
    int amount;
    int numOfPersons;
    double splitAmount;

    GlobalKey<FormState> _formKey = GlobalKey<FormState>();

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text("Bill split app"),
    ),
    body: Padding(
    padding: const EdgeInsets.all(15.0),
    child: Form(
    key: _formKey,
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
    Text(
    splitAmount == null
    ? "Fill the details and click on calculate to get your bill split"
    : "Bill split is ${splitAmount.toStringAsFixed(2)}",
    style: TextStyle(
    fontSize: 25,
    ),
    textAlign: TextAlign.center,
    ),
    Padding(
    padding: const EdgeInsets.symmetric(vertical: 15.0),
    child: TextFormField(
    keyboardType: TextInputType.number,
    validator: (value) {
    print("validator amount value $value");
    if (value.isEmpty) {
    return "Enter some amount";
    } else if (double.parse(value).toInt() <= 0) {
    return "Amount should be greater than 0";
    }
    return null;
    },
    onSaved: (value) {
    print("onSaved amount value $value");
    amount = int.parse(value);
    //int.parse(value);
    },
    decoration: InputDecoration(
    labelText: "Amount",
    hintText: "1000",
    border: OutlineInputBorder(),
    ),
    ),
    ),
    TextFormField(
    validator: (value) {
    print("person value $value");
    if (value.isEmpty) {
    return "Enter number of persons";
    } else if (double.parse(value).toInt() <= 0) {
    return "Number should be greater than 0";
    }
    return null;
    },
    onSaved: (value) {
    print("onsave numOfPersons $value");
    numOfPersons = int.parse(value);
    },
    keyboardType: TextInputType.number,
    decoration: InputDecoration(
    labelText: "Number of persons",
    hintText: "5",
    border: OutlineInputBorder(),
    ),
    ),
    Padding(
    padding: const EdgeInsets.only(top: 15.0),
    child: RaisedButton(
    child: Text(
    "Calculate",
    style: TextStyle(color: Colors.white),
    ),
    onPressed: () {
    if (_formKey.currentState.validate()) {
    _formKey.currentState.save();
    print("calculate $amount");
    print("calculate $numOfPersons");
    setState(() {
    splitAmount = amount / numOfPersons;
    });
    }
    },
    ),
    ),
    ],
    ),
    ),
    ),
    );
    }
    }

    关于validation - 在Flutter中无法在验证器中获得适当的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63913449/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com