解析数据到有状态的小部件

25 浏览
0 Comments

解析数据到有状态的小部件

我正在尝试从一个屏幕解析数据到另一个屏幕,分配屏幕有一个有状态的widget,但是我发现错误说我想要解析的数据未定义,尽管我已经定义了它,请帮忙...谢谢..

这是我的代码,

var data_id;

if (_keyForm.currentState.validate()) {

_keyForm.currentState.save();

// var data = "wmsorders_id='" + data_id + "'";

// var response = await RestService()

// .restRequestServiceGet(SystemParam.URL_UPCOMING, data);

// print("response : " + response.body.toString());

// UpcomingModel upcomingModel;

// upcomingModel = UpcomingModel.fromJson(jsonDecode(response.body));

try {

Navigator.push(

context, MaterialPageRoute(builder: (context) => PickUp(

data_id: data_id

))

);

Toast.show("正在解析: " + data_id, context,);

} catch (e) {

Toast.show("错误: 发生了一些错误", context,

duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);

}

}

}

我尝试在这里解析数据,

class PickUp extends StatefulWidget {

@override

_PickUpState createState() => _PickUpState();

}

class _PickUpState extends State {

var data_id;

PickUp({Key key, this.data_id});

}

我得到了这个错误,

enter image description here

0
0 Comments

在上述代码中,问题的出现是因为在PickUp类中没有添加构造函数。解决方法是在PickUp类中添加构造函数,并在构造函数中接收一个data_id参数。然后,在build方法中可以使用data_id变量。具体代码如下:

class PickUp extends StatefulWidget {

final String data_id;

PickUp({this.data_id});

_PickUpState createState() => _PickUpState();

}

class _PickUpState extends State {

Widget build(BuildContext context) {

return Text(widget.data_id);

}

}

0