简体   繁体   English

它是 dart 中的 substring() 方法中的错误还是错误或其他什么?。 substring() 工作不正常。 有人可以解释原因吗

[英]Is it a Bug or Something wrong in substring() method in dart or something else ?. substring() not working correctly. Can someone explain reason

I have a textfield, which i am validating in such a way that the entered value should have one '.'我有一个文本字段,我正在以这样一种方式验证它,即输入的值应该有一个 '.' (dot or point or decimal). (点或点或小数)。 if another '.'(dot or point or decimal) is encountered then we remove that.如果遇到另一个“.”(点或点或小数),则我们将其删除。 I have written a function for that which uses substring() method.我已经为使用substring()方法的函数编写了一个函数。 It works fine when it has only one '.'当它只有一个 '.' 时它工作正常。 (dot or point or decimal), and also removes the second (点或点或小数),并删除第二个
'.' '.' (dot or point or decimal) when encounterd, works fine and as expected. (点或点或小数)遇到时,按预期工作正常。 but the problem is that when I enter the '.'但问题是当我输入'.' (dot or point or decimal) another time or multiple time. (点或点或小数)另一次或多次。 the substring() method doesn't work at all. substring()方法根本不起作用。 All the logs are printed correctly but substring() mehod is not giving correct logic.所有日志都正确打印,但substring()方法没有给出正确的逻辑。

Here is my code.这是我的代码。

UI Textfield用户界面文本字段

Padding(
  padding: EdgeInsets.all(16),
  child: TextField(
    controller: ctrl,
    onChanged: (text) {
      TextFieldType.isNumeric(text, true, ctrl);
    },
  ))

Logical code is kept in a Class called TextFieldType.逻辑代码保存在名为 TextFieldType 的类中。

class TextFieldType {
  static bool isNumeric(
      String s, bool allowDecimal, TextEditingController controller) {
    if (s == null) return false;
    if (s.isEmpty) return false;

    if (allowDecimal) {
      print("Inside allowDecimal");
      if (s.contains('.')) {
        print("dot is present");
        if ('.'.allMatches(s).length == 1) {
          print("only one dot present");
          return true;
        } else {
          print("Substring of s ->  ${s.substring(0, s.length - 1)}");
          print("length of s -> ${s.length - 1}");
          controller.text = s.substring(0, s.length - 1);
          return false;
        }
      }
      return true;
    }
    return true;
  }
}

在此处输入图片说明

As you can clearly see in the gif that after the 12.55 when I entered the '.'正如您在 gif 中清楚地看到的那样,当我输入 '.' 时,在 12.55 之后。 (dot or point or decimal)then the substring() worked perfectly. (点或点或小数)然后substring()完美地工作。 but when I entered the dot again and again then the substring() function doesn't removed that '.'但是当我一次又一次地输入点时, substring()函数并没有删除那个 '.' (dot or point or decimal). (点或点或小数)。 Why is it behaving like that.为什么会这样。

I created the seperate project and tried in that as well but same problem.我创建了单独的项目并尝试过,但同样的问题。

Here are logs of the function/method.这是函数/方法的日志。

第一

第二

I am using almost similar logic which do not allow dot in textfield.我使用几乎类似的逻辑,不允许在文本字段中使用点。 only numbers are allowed.只允许数字。 it was working good so why this logic is not ??它运行良好,为什么这个逻辑不是?

I request every one to go through this question and have look on it and try to understand and provide a solution which resolves this problem我要求每个人都通过这个问题并查看它并尝试理解并提供解决此问题的解决方案

Thankyou !谢谢 !

第三名

From your question, I could think of one Bruteforce though.从你的问题中,我可以想到一个蛮力。 You could create a boolean variable and initialize it as false.您可以创建一个布尔变量并将其初始化为 false。 Once you iterate through your string characters if you find a '.'一旦你遍历你的字符串字符,如果你找到一个 '.' flag that variable as true and change the state of the application.将该变量标记为 true 并更改应用程序的状态。 if you find any other in that text, replace it with "".如果您在该文本中找到任何其他内容,请将其替换为“”。 I'm not a 100 percent sure this would work but you could try though.我不是 100% 确定这会起作用,但您可以尝试一下。 You could declare a condition like你可以声明一个条件,如

if(thatbool!=true){
// Some Logic
}
else{
//Some Other Logic
}

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

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