skip to Main Content

I need to send data to another screen using dart I try to make constructor on the other screen(editdata.dart) to get the data from the main screen (readdata.dart) it is stateful widget the problem in:
createState() => EditFire() EditFire class need to pass parameters but I don’t have the data yet.

editdata.dart:

class EditData extends StatefulWidget {
  @override

  //Here the Error Occurs//

  State<StatefulWidget> createState() => EditFire();
}

class EditFire extends State<EditData> {
  EditFire(String ID, String date, String time, String dest, String src){
    currentID = ID;
    olddate = date;
    oldtime = time;
    _destinationcontroller.text = dest;
    _sourcecontroller.text = src;
  }
  late String currentID;
  final _destinationcontroller = TextEditingController();
  final _sourcecontroller = TextEditingController();
  late String olddate;
  late String oldtime;
}

in readdata.dart:

EditFire("2","17/6/2022","2:25","NewYork","Japan");

2

Answers


  1. First page

    onPressed: () { 
          Navigator.of(context).push(MaterialPageRoute(builder: (_){
            return EditData("2","17/6/2022","2:25","NewYork","Japan");
          }));
        },
    

    Second page in which you want to receive data

    class EditData extends StatefulWidget {
    
     String currentID;
       String _destinationcontroller;
       String _sourcecontroller;
       String olddate;
       String oldtime;
    
    
       EditData(this.currentID, this._destinationcontroller, this._sourcecontroller,
          this.olddate, this.oldtime, {Key? key}) : super(key: key);
    
      @override
      State<EditData> createState() => _EditDataState();
    }
    
    class _EditDataState extends State<EditData> {
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Center(child:
            Text('${widget.currentID}'),),
        );
      }
    }
    
    Login or Signup to reply.
  2. To access the data of EditData to EditFire, you don’t required to pass it in constructor. As you can see that EditFire is a state of EditData.

    So If you want to get anything in EditFire which is used in EditData, You can used it like widget.abc

    class EditData extends StatefulWidget {
      // here, in constructor of widget, we are gathering values.
      EditData(
          {required this.currentID,
          required this.olddate,
          required this.oldtime,
          Key? key})
          : super(key: key);
      String currentID;
      String olddate;
      String oldtime;
    
      State<StatefulWidget> createState() => EditFire();
    }
    
    // this is a state widget of EditData. so no need to pass value in constructor of state widget.
    
    class EditFire extends State<EditData> {
      final _destinationcontroller = TextEditingController();
      final _sourcecontroller = TextEditingController();
      String? currentID;
      String? olddate;
      String? oldtime;
    
      @override
      void initState() {
        super.initState();
    
        // you can get the values like this
        currentID = widget.currentID;
        olddate = widget.olddate;
        oldtime = widget.oldtime;
      }
    }
    

    And to pass the values to EditData, you need to pass it to the constructor. like,

    EditData(currentID: "", olddate: "", oldtime: "");
    

    Hope, this will resolve your issue, still not resolved please comment with the issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search