Erik Krv
Erik Krv

Reputation: 33

Passing id from one page widget to another page method

I send the id to the second page by clicking where I need to insert the id into the method, but on the second page the id is visible only in the widget and null is displayed in the method. Here I send id to second page.

Navigator.push(context,
MaterialPageRoute(builder: (context) => new JobPage(value: (_jobsDetails.jobs[Index]['jobFullViewDto']['id']).toString())));

value initialization.

final String value;
JobPage({Key key, @required this.value}) : super(key: key);

I can see id in the widget of the second page.

@override
  Widget build(BuildContext context) {print(widget.value);

But i need to put id in the method. In method id is null.

Future<JobDetail> _getAsyncJobDetail() async {
UserService userService = new UserService();
print("widgetId***" + widget.value);
JobDetail details = await userService.jobDetails(widget.value); //null
return details;}

JobPage

class JobPage extends StatefulWidget {
final String value;

JobPage({Key key, @required this.value}) : super(key: key);

@override
_JobPageState createState() => new _JobPageState();

}

class _JobPageState extends State<JobPage> {
JobDetail _jobDetails = new JobDetail();
double pageWidth;
double pageHeight; 

_JobPageState() {
_getAsyncJobDetail().then((val) => setState(() {
      _jobDetails = val;
    }));
}

Future<JobDetail> _getAsyncJobDetail() async {
UserService userService = new UserService();
print("widgetId***" + widget.value);
JobDetail details = await userService.jobDetails(widget.value);
return details;
}

@override
Widget build(BuildContext context) {
print(widget.value);
this.pageWidth = MediaQuery.of(context).size.width - 10;
this.pageHeight = MediaQuery.of(context).size.height;
return Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    Text(
      "${_jobDetails.position}",
      style: TextStyle(
        fontSize: 15,
      ),
    ),
  ],
);

Upvotes: 2

Views: 603

Answers (1)

Mohamad Assem Nasser
Mohamad Assem Nasser

Reputation: 1109

Instead of doing all the pre work in the constructor (which is not recommended) of the State and because your Text widget depends on a Future to be returned, you Should wrap your widget with FutureBuilder.

class JobPage extends StatefulWidget {
  final String value;

  JobPage({Key key, @required this.value}) : super(key: key);

  @override
  _JobPageState createState() => new _JobPageState();

}

class _JobPageState extends State<JobPage> {
  JobDetail _jobDetails = new JobDetail();
  double pageWidth;
  double pageHeight;

  Future<JobDetail> _getAsyncJobDetail() async {
    UserService userService = new UserService();
    print("widgetId***" + widget.value);
    JobDetail details = await userService.jobDetails(widget.value);
    return details;
  }

  @override
  Widget build(BuildContext context) {
    this.pageWidth = MediaQuery.of(context).size.width - 10;
    this.pageHeight = MediaQuery.of(context).size.height;
    return FutureBuilder<JobDetail>(
      future: _getAsyncJobDetail(),
      builder: (BuildContext context, AsyncSnapshot<JobDetail> snapshot) {
        _jobDetails = snapshot;
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return CircularProgressIndicator();
          case ConnectionState.done:
            if (snapshot.hasError)
              return Text('Error: ${snapshot.error}');
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  "${_jobDetails.position}",
                  style: TextStyle(
                    fontSize: 15,
                  ),
                ),
              ],
            );
        }
        return null; // unreachable
      },
    );
  }
}

Upvotes: 1

Related Questions