Home > other >  how can I pass value in setState to class constructor
how can I pass value in setState to class constructor

Time:11-12

I have Error at Over()
This is code:

  Offset _offset;
  GestureDetector(
          onTapDown: (e) {
            Offset offset = e.globalPosition;
            setState(() {
              _offset = offset;
            });

   Positioned(
        left: size.width / 2,
        top: size.height / 2,
        child: CustomPaint(
          painter: Over(offset: _offset),
        ))

Error : The non-nullable local variable '_offset' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path.dartnot_assigned_potentially_non_nullable_local_variable Offset _offset

CodePudding user response:

Use one of the below solutions:

1. Use the late keyword

class x{
  late Offset _offset; // No error
}

2. Make variable nullable

class x{
  Offset? _offset; // No error
}

CodePudding user response:

Try initializing the _offset variable ie. Offset _offset = Offset(0,0)

  • Related