将数据解析为有状态的小部件

时间:2019-11-20 06:58:20

标签: flutter

我正在尝试将屏幕上的数据解析到另一个屏幕,分配屏幕上有状态的小部件,但是我发现错误,尽管我已经定义了要解析的数据,但我没有定义,请帮助。谢谢你。

这是我的代码,

    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("Parsing: " + data_id, context,);
      } catch (e) {
        Toast.show("Error:something wrong", context,
            duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
      }

    }
  }

我尝试在此处解析数据,


class PickUp extends StatefulWidget {
  @override
  _PickUpState createState() => _PickUpState();
}

class _PickUpState extends State<PickUp> {
  var data_id;
  PickUp({Key key, this.data_id});
}

我得到了这个错误, enter image description here

1 个答案:

答案 0 :(得分:1)

在PickUp类中添加构造函数

class PickUp extends StatefulWidget {
  final String data_id;
  PickUp({this.data_id});

  @override
  _PickUpState createState() => _PickUpState();
}

然后您可以在build方法内部使用data_id

@override
  Widget build(BuildContext context) {
    return Text(widget.data_id);
  }