我需要使用dart将数据发送到另一个屏幕,我试图在另一个屏幕上构造函数(editdata.dart)从主屏幕(readdata.dart)获取数据,这是有状态小部件的问题:createState() => EditFire()
EditFire类需要传递参数,但我还没有数据。
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;
}
在readdata.dart:
EditFire("2","17/6/2022","2:25","NewYork","Japan");
首页
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (_){
return EditData("2","17/6/2022","2:25","NewYork","Japan");
}));
},
要接收数据的第二页
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}'),),
);
}
}
访问EditData的数据到EditFire,你不需要在构造函数中传递它。可以看到EditFire是EditData的状态.
所以如果你想在EditFire中获取任何内容在EditData中使用,您可以使用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;
}
}
要将值传递给EditData,需要将其传递给构造函数。如,
EditData(currentID: "", olddate: "", oldtime: "");
希望,这将解决您的问题,仍然没有解决,请评论的问题。