Why does this code cause hitbox exception?

0

Issue

I’ve copied the documentation for StatefulWidget to create this code:

class Note extends StatefulWidget
{
    final String initialValue;

    const Note({Key? key, this.initialValue = ""}) : super(key: key);

    @override
    State<StatefulWidget> createState()
    {
        return NoteState();
    }
}

class NoteState extends State<Note>
{
    String value = "";

    NoteState()
    {
        value = widget.initialValue;
    }

    @override
    Widget build(BuildContext context)
    {
        return Text(value);
    }
}

With a main app scaffold that contains:

body: const Center
(
    child: note_widget.Note(key: Key('key1'), initialValue: 'test text'),
),

When running it I get this looping output below and the label never displays. What am I doing wrong please?

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Cannot hit test a render box that has never been laid out.
The hitTest() method was called on this RenderBox: RenderErrorBox#d7972 NEEDS-LAYOUT NEEDS-PAINT:
  creator: ErrorWidget-[#1866a] ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#583c9 ink renderer] ← NotificationListener<LayoutChangedNotification> ← PhysicalModel ← AnimatedPhysicalModel ← ⋯
  parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body
  constraints: MISSING
  size: MISSING
Unfortunately, this object's geometry is not known at this time, probably because it has never been laid out. This means it cannot be accurately hit-tested.
If you are trying to perform a hit test during the layout phase itself, make sure you only hit test nodes that have completed layout (e.g. the node's children, after their layout() method has been called).
#0      RenderBox.hitTest.<anonymous closure> (package:flutter/src/rendering/box.dart:2380:11)

(If I use child: Text('Hello World') the app runs fine)


Update: The original error occurs before pages of the error listed above and is:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _CastError was thrown building _BodyBuilder:
Null check operator used on a null value

The relevant error-causing widget was:
  Scaffold Scaffold:file:///c:/data/tfjournal/lib/screens/home_screen.dart:11:10

When the exception was thrown, this was the stack:
#0      State.widget (package:flutter/src/widgets/framework.dart:883:26)
#1      new NoteState (package:tfjournal/widgets/note.dart:22:11)
#2      Note.createState (package:tfjournal/widgets/note.dart:12:10)
#3      new StatefulElement (package:flutter/src/widgets/framework.dart:4754:25)

Solution

To update UI context you need to initialize in intestate. Otherwise, you get the null value exception Null check operator used on a null value

Change

  NoteState()
    {
        value = widget.initialValue;
    }

TO

  @override
  void initState() {
    super.initState();
    value = widget.initialValue;
  }

Answered By – Jahidul Islam

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More