Range Error and Backspace Handling Issue in Custom OTP Field Flutter Widget

I am working on a custom-made OTP field widget in Flutter and encountering two issues:

Range Error: I am facing a range error when updating the parent controller with the text entered in the OTP fields.

Backspace Handling: When pressing the backspace button, the previous field should be cleared correctly, but it doesn’t work as expected.

Here is the relevant code for my custom OTP field widget:

    import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class CustomOtpField extends StatefulWidget {
  final TextEditingController controller;
  final int length;
  final double size;
  final double borderRadius;
  final double borderWidth;
  final Color focusedBorderColor;
  final Color unfocusedBorderColor;
  final Color backgroundColor;
  final TextStyle textStyle;
  final double fieldSpacing;

  const CustomOtpField({
    required this.controller,
    this.length = 6,
    this.size = 50.0,
    this.borderRadius = 4.0,
    this.borderWidth = 1.0,
    this.focusedBorderColor = Colors.teal,
    this.unfocusedBorderColor = Colors.grey,
    this.backgroundColor = Colors.white,
    this.textStyle = const TextStyle(fontSize: 20.0, color: Colors.black),
    this.fieldSpacing = 12.0,
    super.key,
  });

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

class CustomOtpFieldState extends State<CustomOtpField> {
  late List<FocusNode> _focusNodes;
  late List<TextEditingController> _fieldControllers;

  @override
  void initState() {
    super.initState();
    _focusNodes = List.generate(widget.length, (index) => FocusNode());
    _fieldControllers =
        List.generate(widget.length, (index) => TextEditingController());
    for (int i = 0; i < widget.length; i++) {
      _fieldControllers[i].addListener(() => _updateParentController(i));
    }
    widget.controller.addListener(_updateOtp);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (_focusNodes.isNotEmpty) {
        _focusNodes[0].requestFocus();
      }
    });
  }

  @override
  void dispose() {
    for (var node in _focusNodes) {
      node.dispose();
    }
    for (var controller in _fieldControllers) {
      controller.dispose();
    }
    widget.controller.removeListener(_updateOtp);
    super.dispose();
  }

  void _updateParentController(int index) {
    final text = _fieldControllers[index].text;
    final currentText = widget.controller.text;

    if (index >= 0 && index < widget.length) {
      String newText;
      if (text.isNotEmpty) {
        newText = (index > 0 ? currentText.substring(0, index) : '') +
            text +
            (index < widget.length - 1 ? currentText.substring(index + 1) : '');
      } else {
        newText = (index > 0 ? currentText.substring(0, index) : '') +
            (index < widget.length - 1 ? currentText.substring(index + 1) : '');
      }

      widget.controller.text = newText;
      widget.controller.selection = TextSelection.fromPosition(
        TextPosition(offset: newText.length),
      );
    }
  }

  void _updateOtp() {
    final text = widget.controller.text;
    if (text.length > widget.length) {
      // Ensure the OTP length does not exceed the specified length
      widget.controller.text = text.substring(0, widget.length);
    }
    if (text.length == widget.length) {
      _focusNodes.last.unfocus();
    } else if (text.length < widget.length) {
      _focusNodes[text.length].requestFocus();
    }
    for (int i = 0; i < widget.length; i++) {
      if (i < text.length) {
        _fieldControllers[i].text = text[i];
      } else {
        _fieldControllers[i].clear();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(widget.length, (index) {
        return Row(
          children: [
            _buildOtpField(index),
            if (index < widget.length - 1) SizedBox(width: widget.fieldSpacing),
          ],
        );
      }),
    );
  }

  Widget _buildOtpField(int index) {
    return SizedBox(
      height: widget.size,
      width: widget.size,
      child: TextField(
        focusNode: _focusNodes[index],
        controller: _fieldControllers[index],
        onChanged: (value) {
          if (value.length <= 1) {
            if (value.isNotEmpty) {
              if (index < widget.length - 1) {
                _focusNodes[index + 1].requestFocus();
              }
            } else {
              if (index > 0) {
                _focusNodes[index - 1].requestFocus();
              }
            }
            _updateParentController(index);
          }
        },
        textAlign: TextAlign.center,
        maxLength: 1,
        keyboardType: TextInputType.number,
        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
        decoration: InputDecoration(
          counterText: '',
          contentPadding: EdgeInsets.zero,
          filled: true,
          fillColor: widget.backgroundColor,
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(widget.borderRadius),
            borderSide: BorderSide(
              color: _focusNodes[index].hasFocus
                  ? widget.focusedBorderColor
                  : widget.unfocusedBorderColor,
              width: widget.borderWidth,
            ),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(widget.borderRadius),
            borderSide: BorderSide(
              color: widget.focusedBorderColor,
              width: widget.borderWidth,
            ),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(widget.borderRadius),
            borderSide: BorderSide(
              color: widget.unfocusedBorderColor,
              width: widget.borderWidth,
            ),
          ),
        ),
        style: widget.textStyle,
      ),
    );
  }
}

Issues:

Range Error: I encounter a range error when updating the parent controller with the text entered in the OTP fields. How can I handle this without causing a range error?

Backspace Handling: When pressing the backspace button, the previous field should be cleared correctly, but it doesn’t work as expected. How can I fix this issue?

Additional Context:

I want to make sure that backspace correctly clears the current field and moves the focus to the previous field if needed.
The TextEditingController for each OTP field is managed within the widget.
Thank you for your help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật