Home > Mobile >  How to fix issue with Late in flutter/dart
How to fix issue with Late in flutter/dart

Time:08-22

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.)

CodePudding user response:

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

CodePudding user response:

replace

void iniState()

with

void initState()
  • Related