jifalops / datetime_picker_formfield

A Flutter widget that wraps a TextFormField and integrates the date and/or time picker dialogs.
MIT License
186 stars 101 forks source link

Error: Failed to handle method call #110

Open egncndr opened 3 years ago

egncndr commented 3 years ago

Hello,

Im seeing some error in the logs (Please see error below) while manually inputing date via the date picker text box. Below are the steps to reproduce.

Steps to reproduce:

  1. Implement Date Picker (Please see below for my sample usage)
  2. Open date picker
  3. Tap the edit icon
  4. Enter date manually (as per my format dd/MM/yyyy)
  5. Then press Ok

After pressing Ok, the logs below will show.

Although my app is functioning just wondering what is the cause of the said error.

Thanks Eugene

Error:

E/MethodChannel#flutter/textinput( 8538): Failed to handle method call
E/MethodChannel#flutter/textinput( 8538): java.lang.IndexOutOfBoundsException: invalid selection start: 10
E/MethodChannel#flutter/textinput( 8538):   at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.<init>(TextInputChannel.java:724)
E/MethodChannel#flutter/textinput( 8538):   at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.fromJson(TextInputChannel.java:680)
E/MethodChannel#flutter/textinput( 8538):   at io.flutter.embedding.engine.systemchannels.TextInputChannel$1.onMethodCall(TextInputChannel.java:91)
E/MethodChannel#flutter/textinput( 8538):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#flutter/textinput( 8538):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#flutter/textinput( 8538):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
E/MethodChannel#flutter/textinput( 8538):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter/textinput( 8538):   at android.os.MessageQueue.next(MessageQueue.java:335)
E/MethodChannel#flutter/textinput( 8538):   at android.os.Looper.loop(Looper.java:183)
E/MethodChannel#flutter/textinput( 8538):   at android.app.ActivityThread.main(ActivityThread.java:7660)
E/MethodChannel#flutter/textinput( 8538):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter/textinput( 8538):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/MethodChannel#flutter/textinput( 8538):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 8538): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, invalid selection start: 10, null, java.lang.IndexOutOfBoundsException: invalid selection start: 10
E/flutter ( 8538):  at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.<init>(TextInputChannel.java:724)
E/flutter ( 8538):  at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.fromJson(TextInputChannel.java:680)
E/flutter ( 8538):  at io.flutter.embedding.engine.systemchannels.TextInputChannel$1.onMethodCall(TextInputChannel.java:91)
E/flutter ( 8538):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter ( 8538):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter ( 8538):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
E/flutter ( 8538):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter ( 8538):  at android.os.MessageQueue.next(MessageQueue.java:335)
E/flutter ( 8538):  at android.os.Looper.loop(Looper.java:183)
E/flutter ( 8538):  at android.app.ActivityThread.main(ActivityThread.java:7660)
E/flutter ( 8538):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 8538):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter ( 8538):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 8538): )
E/flutter ( 8538): #0      JSONMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:158:7)
E/flutter ( 8538): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
E/flutter ( 8538): <asynchronous suspension>
E/flutter ( 8538): 

Code Implementation

Scaffold(
    body: SingleChildScrollView(
        padding: EdgeInsetsDirectional.fromSTEB(20, 20, 20, 20),
        child: FutureBuilder<String>(
        future: _fCustomerId,
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
            switch (snapshot.connectionState) {
            case ConnectionState.waiting:
                return Container(alignment: Alignment.center, child: CircularProgressIndicator());
            default:
                if (snapshot.hasError) {
                return Container(
                    alignment: Alignment.center,
                    child: Text('Error: ${snapshot.error}'));
                } else {
                _customerDetails.id = snapshot.data;
                return content();
                }
            }
        },
        )),
);

Widget content() {
    return Container(
      width: size.width,
      child:Form(
              key: _formKey,
              child: DateTimeField(
                autovalidateMode: AutovalidateMode.onUserInteraction,
                decoration: InputDecoration(
                    fillColor: Colors.white,
                    filled: true,
                    isDense: true,
                    hintText: 'Date of Birth',
                    border: OutlineInputBorder(),
                    labelText: 'Date of Birth',
                ),
                format: DateFormat('dd/MM/yyyy'),
                onShowPicker: (context, currentValue) {
                    return showDatePicker(
                        context: context,
                        firstDate: DateTime(1900),
                        initialDate: currentValue ?? DateTime.now(),
                        lastDate: DateTime(2100));
                },
                )
        )
    );
}
EthanShoeDev commented 3 years ago

I am also having this issue. Exactly as described.

dninet commented 3 years ago

I am also having this issue, does this have something to do with locales?

EthanShoeDev commented 3 years ago

I am also having this issue, does this have something to do with locales?

After looking further into it, I believe it is caused when the date entered has a removable '0' in it. For example if the user types in '11/04/21' the zero next the four can be removed. At some point (I forget exactly where and I am not near my pc) when converting the string to a date time object, these unnecessary 0's get removed.

After this happens the code tries to move the text editing selector to the end of the string. The only problem is that it uses the length of the string before the 0's were removed. Trying to move the selection pointer past the end of the string causes the exception in question. "PlatformException(error, invalid selection start: 10"

I actually found and fixed the bug but my documentation wasn't good enough so the flutter team rejected my PR.

dninet commented 3 years ago

@SherlockShoe can confirm, it works if I abstain from leading zeroes

Maatteogekko commented 2 years ago

Just found this issue by googling. It happens in my app even though I don't use this package, so it must be Flutter's bug. @SherlockShoe could you link the PR that was rejected? Maybe we can get it to be approved.

EthanShoeDev commented 2 years ago

I can't seem to find the code. I initially made an issue to link to but maybe I never submitted the code fix. https://github.com/flutter/flutter/issues/80571

The issue was something compressing the date string on the dart side and then asking the platform side to move the cursor to index 10.