1

Please check this video.

Here I extend TextEditingController to highlight(also clickable) the keyword "ali". It is working perfectly whenever the user types "ali". But when initializing text to the controller, it is showing a lot of errors. Someone pls help me with how I can resolve this error.

Note: When I initialize the text and remove "recognizer" from "textspan" it is working perfectly without showing any error. I guess this issue is happening because of "recognizer" in the textspan.

here is my code, pls uncomment line number 15 to see the error.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:kyubook/utils/utility.dart';

class TexspanTap extends StatefulWidget {
  @override
  _TexspanTapState createState() => _TexspanTapState();
}

class _TexspanTapState extends State<TexspanTap> {
  final MyTextEditingcontroller _editingcontroller = MyTextEditingcontroller();

  @override
  void initState() {
    //_editingcontroller.text = "dghali";
    super.initState();
  }

  @override
  void dispose() {
    _editingcontroller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SafeArea(
        child: Column(
          children: [
            TextField(
              controller: _editingcontroller,
            ),
          ],
        ),
      ),
    );
  }
}

class MyTextEditingcontroller extends TextEditingController {
  @override
  TextSpan buildTextSpan(
      {required BuildContext context,
      TextStyle? style,
      required bool withComposing}) {
    if (text.toLowerCase().contains("ali")) {
      final List<InlineSpan> spans = [];

      final int indexOfAli = text.indexOf("ali");

      spans.add(TextSpan(text: text.substring(0, indexOfAli)));

      spans.add(
        TextSpan(
            text: text.substring(indexOfAli, indexOfAli + 3),
            style: const TextStyle(color: Colors.red),
            recognizer: TapGestureRecognizer()
              ..onTapDown = (final details) {
                commonToast("Touched");
              }),
      );

      spans.add(TextSpan(text: text.substring(indexOfAli + 3, text.length)));

      return TextSpan(children: spans, style: style);
    }

    return TextSpan(text: text, style: style);
  }
}

Logs

I/flutter (26328): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (26328): The following assertion was thrown building
I/flutter (26328): RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#87b14](state:
I/flutter (26328): RawGestureDetectorState#ede1a(gestures: <none>, excludeFromSemantics: true, behavior: opaque)):
I/flutter (26328): 'package:flutter/src/rendering/editable.dart': Failed assertion: line 2376 pos 14: 'readOnly &&
I/flutter (26328): !obscureText': is not true.
I/flutter (26328): 
I/flutter (26328): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (26328): more information in this error message to help you determine and fix the underlying cause.
I/flutter (26328): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (26328):   https://github.com/flutter/flutter/issues/new?template=2_bug.md
I/flutter (26328): 
I/flutter (26328): The relevant error-causing widget was:
I/flutter (26328):   TextField
I/flutter (26328):   file:///Users/ali/Documents/Flutter/Xcelpros/kb_main_clientside/lib/components/profanity_checker/screen/textspan_tap.dart:32:13
I/flutter (26328): 
I/flutter (26328): When the exception was thrown, this was the stack:
I/flutter (26328): #2      RenderEditable.describeSemanticsConfiguration (package:flutter/src/rendering/editable.dart:2376:14)
I/flutter (26328): #3      RenderObject._semanticsConfiguration (package:flutter/src/rendering/object.dart:2525:7)
I/flutter (26328): #4      RenderObject.attach (package:flutter/src/rendering/object.dart:1416:34)
I/flutter (26328): #5      RelayoutWhenSystemFontsChangeMixin.attach (package:flutter/src/rendering/object.dart:3401:11)
I/flutter (26328): #6      RenderEditable.attach (package:flutter/src/rendering/editable.dart:2612:11)
I/flutter (26328): #7      AbstractNode.adoptChild (package:flutter/src/foundation/node.dart:138:13)
I/flutter (26328): #8      RenderObject.adoptChild (package:flutter/src/rendering/object.dart:1274:11)
I/flutter (26328): #9      RenderObjectWithChildMixin.child= (package:flutter/src/rendering/object.dart:3022:7)
I/flutter (26328): #10     SingleChildRenderObjectElement.insertRenderObjectChild (package:flutter/src/widgets/framework.dart:6098:18)
I/flutter (26328): #11     RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:5758:35)
I/flutter (26328): #12     RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5440:5)
I/flutter (26328): ...     Normal element mounting (92 frames)
I/flutter (26328): #104    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #105    Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
I/flutter (26328): #106    _DecorationElement._mountChild (package:flutter/src/material/input_decorator.dart:1591:31)
I/flutter (26328): #107    _DecorationElement.mount (package:flutter/src/material/input_decorator.dart:1604:5)
I/flutter (26328): ...     Normal element mounting (69 frames)
I/flutter (26328): #176    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #177    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
I/flutter (26328): #178    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
I/flutter (26328): ...     Normal element mounting (34 frames)
I/flutter (26328): #212    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #213    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
I/flutter (26328): #214    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
I/flutter (26328): ...     Normal element mounting (238 frames)
I/flutter (26328): #452    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #453    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
I/flutter (26328): #454    Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
I/flutter (26328): #455    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5654:32)
I/flutter (26328): #456    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6243:17)
I/flutter (26328): #457    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #458    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #459    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
I/flutter (26328): #460    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #461    StatefulElement.update (package:flutter/src/widgets/framework.dart:4778:5)
I/flutter (26328): #462    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #463    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #464    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #465    ProxyElement.update (package:flutter/src/widgets/framework.dart:4922:5)
I/flutter (26328): #466    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #467    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #468    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #469    ProxyElement.update (package:flutter/src/widgets/framework.dart:4922:5)
I/flutter (26328): #470    _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
I/flutter (26328): #471    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #472    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6090:14)
I/flutter (26328): #473    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #474    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #475    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
I/flutter (26328): #476    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #477    StatefulElement.update (package:flutter/src/widgets/framework.dart:4778:5)
I/flutter (26328): #478    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #479    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6090:14)
I/flutter (26328): #480    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #481    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6090:14)
I/flutter (26328): #482    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #483    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #484    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #485    ProxyElement.update (package:flutter/src/widgets/framework.dart:4922:5)
I/flutter (26328): #486    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #487    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #488    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
I/flutter (26328): #489    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #490    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2582:33)
I/flutter (26328): #491    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:875:21)
I/flutter (26328): #492    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
I/flutter (26328): #493    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
I/flutter (26328): #494    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
I/flutter (26328): #495    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:998:5)
I/flutter (26328): #499    _invoke (dart:ui/hooks.dart:163:10)
I/flutter (26328): #500    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:259:5)
I/flutter (26328): #501    _drawFrame (dart:ui/hooks.dart:126:31)
I/flutter (26328): (elided 5 frames from class _AssertionError and dart:async)
I/flutter (26328): 
I/flutter (26328): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (26328): Another exception was thrown: LateInitializationError: Field '_tap@798245603' has not been initialized.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 2 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 2 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 1 line
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 3 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 2 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 1 line
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: A GlobalKey was used multiple times inside one widget's child list.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 18 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
Ali Akbar
  • 21
  • 1
  • 4
  • instead `TextSpan` why dont you use `WidgetSpan` where you need "clickable" area? – pskink Jan 29 '22 at 09:22
  • @pskink WidgetSpan doesn't work with TextEditingController. – Ali Akbar Jan 31 '22 at 04:12
  • @pskink it is not working, still raising errors. – Ali Akbar Feb 01 '22 at 04:38
  • @pskink, could you pls watch this [screenrecord](https://drive.google.com/file/d/1lLCI91TeYxo8LDqMsqkQcIUO4GnhB-uc/view?usp=sharing). here when I start typing `@` it raises errors. I don't use any other code here. – Ali Akbar Feb 01 '22 at 06:16
  • @pskink thank you for your effort. however, still, it is not working. i just paste the code that you shared. here pls check this [screenrecord](https://drive.google.com/file/d/1lLCI91TeYxo8LDqMsqkQcIUO4GnhB-uc/view?usp=sharing) – Ali Akbar Feb 01 '22 at 06:40
  • @pskink in the web also it is showing error. – Ali Akbar Feb 01 '22 at 07:25
  • @pskink I'm using Flutter 2.2.2. also tied to latest flutter version, it also showing error – Ali Akbar Feb 01 '22 at 07:51
  • @pskink actually issue is not with that `trim()`, it is because of using WidgetextSpan in TextEditingController. If you don't mind can you share with me a screen recording of the output of working code? – Ali Akbar Feb 01 '22 at 10:01

1 Answers1

0

TextSpan within TextSpan with recognizer only work in RichText widget. See here

So in short, you need RichText in your TextField. But TextFields don't support RichText as of now. You need some package which can make it happen. I found rich_input.

Give it a try if this can help you out.

Aakash Kumar
  • 1,069
  • 7
  • 11
  • TextField supports TextSpans - that's why TextEditingController has buildTextSpan method – pskink Jan 28 '22 at 15:14
  • @pskink Yes, TextField supports TextSpans. But as I say it doesn't support RichText. RichText is needed if you want to use nested TextSpan with individual recognizer. – Aakash Kumar Jan 28 '22 at 15:43
  • yes, you are right, i got you wrong... sorry, my fault – pskink Jan 28 '22 at 16:09
  • 1
    Thank you for responding @AakashKumar. Actually, TextSpan within TextSpan is working perfectly with a recognizer in TextEditingController. The problem is , if we initialize the value while the corresponding Textfield is not rendered, then only it is raising the error. you can see the above mentioned video, when I type keyword "ali", it is highlighting and also clickable, but if I initialize value before rendering the corresponding TextEditingController Textfield, then it is showing errors. Also, I tried 'rich_input', I think this package doesn't provide the option for 'recognizer'. – Ali Akbar Jan 29 '22 at 05:39