TextEditingControllerの使い方を学ぶため、Flutterの公式サイトからコードをコピペしAndroid studioで実行したところ、TextFormField
をタップした瞬間に無限ループが発生しました。
/// Flutter code sample for TextEditingController // This example creates a [TextField] with a [TextEditingController] whose // change listener forces the entered text to be lower case and keeps the // cursor at the end of the input. import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); /// This is the main application widget. class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); static const String _title = 'Flutter Code Sample'; @override Widget build(BuildContext context) { return const MaterialApp( title: _title, home: MyStatefulWidget(), ); } } /// This is the stateful widget that the main application instantiates. class MyStatefulWidget extends StatefulWidget { const MyStatefulWidget({Key? key}) : super(key: key); @override State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); } /// This is the private State class that goes with MyStatefulWidget. class _MyStatefulWidgetState extends State<MyStatefulWidget> { final TextEditingController _controller = TextEditingController(); // 中身が更新されると呼ばれる関数。ここで無限ループしてる? @override void initState() { super.initState(); _controller.addListener(() { final String text = _controller.text.toLowerCase(); _controller.value = _controller.value.copyWith( text: text, selection: TextSelection(baseOffset: text.length, extentOffset: text.length), composing: TextRange.empty, ); }); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: Container( alignment: Alignment.center, padding: const EdgeInsets.all(6), child: TextFormField( controller: _controller, decoration: const InputDecoration(border: OutlineInputBorder()), ), ), ); } }
console
1Syncing files to device sdk gphone x86 arm... 2W/IInputConnectionWrapper(24578): beginBatchEdit on inactive InputConnection 3W/IInputConnectionWrapper(24578): endBatchEdit on inactive InputConnection 4I/TextInputPlugin(24578): Composing region changed by the framework. Restarting the input method. 5W/IInputConnectionWrapper(24578): getTextBeforeCursor on inactive InputConnection 6W/IInputConnectionWrapper(24578): getSelectedText on inactive InputConnection 7W/IInputConnectionWrapper(24578): getTextAfterCursor on inactive InputConnection 8I/TextInputPlugin(24578): Composing region changed by the framework. Restarting the input method. 9I/TextInputPlugin(24578): Composing region changed by the framework. Restarting the input method. 10W/IInputConnectionWrapper(24578): getTextBeforeCursor on inactive InputConnection 11W/IInputConnectionWrapper(24578): getSelectedText on inactive InputConnection 12W/IInputConnectionWrapper(24578): getTextAfterCursor on inactive InputConnection 13W/IInputConnectionWrapper(24578): getTextBeforeCursor on inactive InputConnection 14W/IInputConnectionWrapper(24578): getSelectedText on inactive InputConnection 15W/IInputConnectionWrapper(24578): getTextAfterCursor on inactive InputConnection 16I/TextInputPlugin(24578): Composing region changed by the framework. Restarting the input method. 17W/IInputConnectionWrapper(24578): getTextBeforeCursor on inactive InputConnection 18W/IInputConnectionWrapper(24578): getSelectedText on inactive InputConnection 19W/IInputConnectionWrapper(24578): getTextAfterCursor on inactive InputConnection 20I/TextInputPlugin(24578): Composing region changed by the framework. Restarting the input method. 21W/IInputConnectionWrapper(24578): getTextBeforeCursor on inactive InputConnection 22W/IInputConnectionWrapper(24578): getSelectedText on inactive InputConnection 23W/IInputConnectionWrapper(24578): getTextAfterCursor on inactive InputConnection 24I/TextInputPlugin(24578): Composing region changed by the framework. Restarting the input method. 25W/IInputConnectionWrapper(24578): getTextBeforeCursor on inactive InputConnection 26...
同じような処理文が規則性なく発生している状況です。
入力した文字列を小文字化してカーソルを最後尾に移動させる、という処理内容だと認識してます。
公式の本文にはリスナー内の処理(今回の場合は_controller.addListener(){}
)で無限ループが発生する可能性があるため注意が必要、という旨の文章があるのですが、サンプルがすでに無限ループが発生しているため、解決方法がわかりません。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。