Flutter - spans cannot have a zero length

6,656

Solution 1

I think the full error might be somewhere else. I would suggest opening a new flutter issue if the issue is specific to one android device.

Solution 2

The fix for me was to add autocorrect: false, and set keyboardType: TextInputType.visiblePassword the bug occurred on my s8plus, it didn't happen on my iPhone simulator.

return TextFormField(
        controller: emailController,
        autofocus: true,
        autocorrect: false,
        keyboardType: TextInputType.visiblePassword,
        validator: validateEmailOrPhoneNumber,
        onChanged: (text) {
          this._validateEmailORPhoneNumber(text);
          this._onTouched('emailOrPhoneNumber');
        },
        onTap: () => _onTouched('emailOrPhoneNumber'),
        decoration: InputDecoration(
            prefixIcon: isPhoneNumber
                ? Container(
                    width: 70,
                    child: leftSection,
            )
                : null,
            labelText: 'Email or Phone number',
            hintText: 'Enter Email or Phone number'),
      );

Solution 3

I had the same error. In my case, the problem wasn't the TextFormField. It was because of the key inside the Form widget. If you use a key, the general widget must be StatefulWidget, not StatelessWidget.

Wrong code

class MyWidget extends StatelessWidget{
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); 
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Form(
        key: _formKey,
        child: TextFormField(),
      ),
    );
  }
}

Right Code

class MyWidget extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyWidgetState();
  }
}

class _MyWidgetState extends State<MyWidget>{
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); 
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Form(
        key: _formKey,
        child: TextFormField(),
      ),
    );
  }
}
Share:
6,656
Alexi Coard
Author by

Alexi Coard

Full Stack Developer at Rise Up (riseup.ai)

Updated on December 02, 2022

Comments

  • Alexi Coard
    Alexi Coard over 1 year

    I am developping a Flutter app which uses a Textfield.

    I am declaring the TextField like this :

    new TextField(
      controller : _controller,
      decoration : new InputDecoration(
          hintText: 'Message...'
      )
    )
    

    The TextField is displayed in my widget, however when I tap on it, the keyboard is automatically closed and the following error appears in the console

    E/SpannableStringBuilder(17860): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    

    I am running this using the Flutter plugin in IntelliJ on a Android (Samsung Galaxy Grand Prime, Android 5.1).

    How could I solve this problem ?

    EDIT: As seen on this answer (Android - SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length), I have tried switching keyboard (Google and Samsung), stille the same

  • Alexi Coard
    Alexi Coard about 7 years
    Will try with another android device, I will keep you in touch
  • Alexi Coard
    Alexi Coard about 7 years
    Just tried with a Galxy Tab A and doesn't get the bug. Will open an issue. Thanks collin