Search code examples
flutterdartvoid

How to fix issue with Late in flutter/dart


I have this code in flutter

late String firstHalf;
  late String secondHalf;

  bool hiddenText = true;
  double textHeight = Diamension.screenHeight / 5.63;

  @override
  void iniState() {
    super.initState();
    if (widget.text.length > textHeight) {
      firstHalf = widget.text.substring(0, textHeight.toInt());
      secondHalf =
          widget.text.substring(textHeight.toInt() + 1, widget.text.length);
    } else {
      firstHalf = widget.text;
      secondHalf = "";
    }
  }

from the code you will see that I initialized "secondHalf" but I kept getting this error in debug

Exception has occurred.
LateError (LateInitializationError: Field 'secondHalf' has not been initialized.)

Solution

  • Unless you copy-pasted wrong into StackOverflow, you have just missed a t...

    Try changing void iniState() { to void initState() {

    You probably have this warning (as also noted by @julemand101):

    The method doesn't override an inherited method. Try updating this class to match the superclass, or removing the override annotation.dartoverride_on_non_overriding_member