code_field
code_field copied to clipboard
RangeError when setting single letter to CodeController
When you set single letter to CodeController, it throws this error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: RangeError (index): Invalid value: Only valid value is 0: -1
#0 _StringBase.[] (dart:core-patch/string_patch.dart:260:41)
#1 CodeController.value=
package:code_text_field/src/code_controller.dart:166
#2 TextEditingController.text=
package:flutter/…/widgets/editable_text.dart:136
#3 _MyAppState.initState.<anonymous closure>
package:class_to_record/maina.dart:21
#4 new Future.delayed.<anonymous closure> (dart:async/future.dart:393:39)
#5 _rootRun (dart:async/zone.dart:1420:47)
#6 _CustomZone.run (dart:async/zone.dart:1328:19)
#7 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
#8 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
#9 _rootRun (dart:async/zone.dart:1428:13)
#10 _CustomZone.run (dart:async/zone.dart:1328:19)
#11 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
#12 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
#13 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
#14 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
#15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
This doesn't happen when you set either 0 or more than 1.
Repro:
import 'package:code_text_field/code_text_field.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _controller = CodeController();
@override
void initState() {
super.initState();
_controller.text = 'x';
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CodeField(
controller: _controller,
),
),
);
}
}
EDIT: something weird is happening if you "redirect" one field to another. If you do this:
late final _controller1 = CodeController(onChange: (t) => _controller2.text = t);
final _controller2 = CodeController();
Column(
children: [
CodeField(controller: _controller1),
CodeField(controller: _controller2),
],
),
If you start writing in the first field, it doesn't write the first letter (throws error), after you press another, it writes both. Then it again doesn't write (throws error), then again writes both.