简体   繁体   中英

RangeError (index): Invalid value: Not in inclusive range 0..13: 14 Flutter

Someone to help me? I'm a beginner. I'm trying to get the controller numbers from a textfield. For this I tried to convert the characters to int. Note: in the textfield the user will enter numbers up to three digits This error appears: RangeError (index): Invalid value: Not in inclusive range 0..13: 14

validate(textController)
  {
    List textCode = List();
    List messageInCode = List();
    int validateRepeticions;
    var stringText = textController;

    for (var b = 0; b < stringText.length; b++)
    {
      textCode.insert(b, stringText[b]);
    }

    validateRepeticions = textCode.length;
    print(validateRepeticions);
    int g = 0;
    int k = 0;
    
    do 
    {
      
      bool comma = true;
      List tryToParseInt = List();
      int d = 0;// 'd' is responsible for identifying the positional value of a digit
      int a = 1;// 'a' increment values to k
      int c = 0;// 'c' defines the positions used in tryToParseInt
      int completeNumber;

        tryToParseInt.insert(c, int.tryParse(textCode[k]));

        if (tryToParseInt[c] == null)
        {
          validateRepeticions--;
        }
        else
        {
          d++;
          c++;
          do
          {
            tryToParseInt.insert(c, int.tryParse(textCode[k+a]));

            if (tryToParseInt[c] == null)
            {
              completeNumber = tryToParseInt[0];
              messageInCode.insert(g, completeNumber);
              g++;
              comma = false;
              validateRepeticions -= 2;
            }
            else
            {
              d++;
              a++;
              c++;
              tryToParseInt.insert(c, int.tryParse(textCode[k+a]));

              if (tryToParseInt[c] == null)
              {
                completeNumber = tryToParseInt[0]*10 + tryToParseInt[1];
                messageInCode.add(completeNumber);
                g++;
                comma = false;
                validateRepeticions -= 3;
                d++;
              }
              else
              {
                d++;
                completeNumber = tryToParseInt[0]*100 + tryToParseInt[1]*10 + tryToParseInt[2];
                messageInCode.add(completeNumber);
                g++;
                comma = false;
                validateRepeticions -= 3;
              }
            }
          } while (comma);
        }
        d++;
        k += d;
    } while (validateRepeticions >= 0);
  }

Updated code

validate(textEditingController) {
  int validateRepeticions = textEditingController.text.length;
  List textString = textEditingController.text.split('');
  List messageInCode = List();
  int k = 0;

  do {
    bool comma = true;
    List tryToParseInt = List();
    int d = 0; // 'd' is responsible for identifying the value of a number that has already been verified
    int a = 1; // 'a' increment values to k
    int c = 0; // 'c' defines the positions used in tryToParseInt
    int completeNumber;

    tryToParseInt.insert(c, int.tryParse(textString[k]));

    if (tryToParseInt[c] == null) {
      validateRepeticions--;
    } else {
      d++; //1
      c++; //1
      do {
        tryToParseInt.insert(c, int.tryParse(textString[k + a]));

        if (tryToParseInt[c] == null) {
          completeNumber = tryToParseInt[0];
          messageInCode.add(completeNumber);
          comma = false;
          validateRepeticions -= 2;
        } else {
          d++; //2
          a++; //2
          c++; //2
          tryToParseInt.insert(c, int.tryParse(textString[k + a]));

          if (tryToParseInt[c] == null) {
            completeNumber = tryToParseInt[0] * 10 + tryToParseInt[1];
            messageInCode.add(completeNumber);
            comma = false;
            validateRepeticions -= 3;
            d++;
          } else {
            d++;
            completeNumber = tryToParseInt[0] * 100 + tryToParseInt[1] * 10 + tryToParseInt[2];
            messageInCode.add(completeNumber);
            comma = false;
            validateRepeticions -= 3;
          }
        }
      } while (comma);
    }
    d++;
    k += d;
  } while (validateRepeticions > 0);
}

Now it's almost working, although an error still appears: The method '_addFromInteger' was called on null. Receiver: null Tried calling: _addFromInteger(0)

validate(textEditingController) {
  int validateRepeticions = textEditingController.text.length;
  List textString = textEditingController.text.split('');
  List messageInCode = List();
  int k = 0;
  messageInCode.clear();

  do {
    bool comma = true;
    List tryToParseInt = List();
    int d = 0; // 'd' is responsible for identifying the positional value of a digit
    int a = 0; // 'a' increment values to k
    int c = 0; // 'c' defines the positions used in tryToParseInt
    int completeNumber;

    if (k < textEditingController.text.length) {
      tryToParseInt.insert(c, int.tryParse(textString[k]));

      if (tryToParseInt[c] == null) {
        validateRepeticions--;
      } else {
        d++; //1 these comments are examples
        c++; //1
        a++; //1

        do {
          if ((k + a) < textEditingController.text.length) {
            tryToParseInt.insert(c, int.tryParse(textString[k + a]));

            if (tryToParseInt[c] == null) {
              completeNumber = tryToParseInt[0];
              messageInCode.add(completeNumber);
              comma = false;
              validateRepeticions -= 2;
            } else {
              d++; //2
              a++; //2
              c++; //2

              if ((k + a) < textEditingController.text.length) {
                tryToParseInt.insert(c, int.tryParse(textString[k + a]));

                if (tryToParseInt[c] == null) {
                  completeNumber = tryToParseInt[0] * 10 + tryToParseInt[1];
                  messageInCode.add(completeNumber);
                  comma = false;
                  validateRepeticions -= 3;
                  d++;
                } else {
                  d++;
                  completeNumber = tryToParseInt[0] * 100 + tryToParseInt[1] * 10 + tryToParseInt[2];
                  messageInCode.add(completeNumber);
                  comma = false;
                  validateRepeticions -= 3;
                }
              } else {
                completeNumber = tryToParseInt[0] * 10 + tryToParseInt[1];
                messageInCode.add(completeNumber);
                comma = false;
                validateRepeticions -= 3;
                d++;
              }
            }
          } else {
            completeNumber = tryToParseInt[0];
            messageInCode.add(completeNumber);
            comma = false;
            validateRepeticions -= 2;
          }
        } while (comma);
      }
    } else {
      validateRepeticions--;
    }
    d++;
    k += d;
  } while (validateRepeticions > 0 && k < textEditingController.text.length);
}

Replace textCode.insert(b, stringText[b]); with textCode.add(stringText[b]);

In your function, I'm assuming the param textController is the TextEditingController . In that case you need to check the length with textController.text.length

To create a list of characters from the string, you just need to use split() instead of creating a new list and loop through the string. For example: textController.text.split('')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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