'I'm creating and validating a dynamic form in Flutter, but when I change the page and return to the previous page I get an error. how can i solve

It works in release mode but I get an error in debug mode

class UrunHizmetWidget extends StatefulWidget {
  int index;
  final state = _UrunHizmetWidgetState();
  UrunHizmetWidget({Key? key, this.index = 0}) : super(key: key);

  @override
  State<UrunHizmetWidget> createState() => state;

  bool isvalid() => state.validator();
}

**Error Message **

The following assertion was thrown building NotificationListener<KeepAliveNotification>:
The createState function for UrunHizmetWidget returned an old or invalid state instance: UrunHizmetWidget, which is not null, violating the contract for createState.
'package:flutter/src/widgets/framework.dart':
package:flutter/…/widgets/framework.dart:1
Failed assertion: line 4860 pos 7: 'state._widget == null'


Solution 1:[1]

I hope this could help you to solve your problem

 class UrunHizmetWidget extends StatefulWidget  {
     int index;
     var state = _UrunHizmetWidgetState();
    
     UrunHizmetWidget ({Key key, this.site, this.index=0}) : super(key: key);
    
     @override
     State<UrunHizmetWidget> createState(){
          return this.state=new __UrunHizmetWidgetState();
     };
    
      bool isvalid() => state.validator();
     }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mj darvishi