Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - spans cannot have a zero length

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

like image 678
Alexi Coard Avatar asked May 21 '17 11:05

Alexi Coard


3 Answers

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.

like image 129
Collin Jackson Avatar answered Nov 08 '22 22:11

Collin Jackson


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'),
      );
like image 41
Obinna Maduabum Avatar answered Nov 08 '22 21:11

Obinna Maduabum


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(),
      ),
    );
  }
}
like image 2
Deve Avatar answered Nov 08 '22 22:11

Deve