Passing data and receiving result from one screen to other in flutter



Passing data and receiving result and Callback  from one screen to other in flutter is quite simple


Passing Data from one screen to other

Step 1- Navigate to second class


_onCallToSecondClass(){
    Navigator.push(context, MaterialPageRoute(builder: (context) => SecondClass('Abc',123)),);
  }



Step 2- Second Class


class SecondClass extends StatefulWidget {
  final String userName;
  final int rNumber;
  SecondClass(this.userName, this.rNumber);

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




Now Passing Data and Getting activity result back to First screen

Step 1- Navigate to second class


  final result =await Navigator.push(context, MaterialPageRoute(builder: (context) => SecondClass('Abc',123)));


 Step 2- Back Result to first class


 Navigator.pop(context,'result');

 

Example-


 _onCallToSecondClass() async {
    final result =await Navigator.push(context, MaterialPageRoute(builder: (context) => SecondClass('Abc',123)));
    int rNumber= result as int;
    print(rNumber);
  }


class SecondClass extends StatefulWidget {
  final String userName;
  final int rNumber;
  SecondClass(this.userName, this.rNumber);
    @override
  _SecondClassState createState() => _SecondClassState();
}

class _SecondClassState extends State<SecondClass> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Title'),
          centerTitle: true,
          backgroundColor: AppTheme.appBarColor,
        ),
        body: FlatButton(
            onPressed: (){
           Navigator.pop(context,widget.rNumber);
         },
            child: Text('Return Your Result ${widget.userName}'))
    );
  }
}






Manage Callbacks

Step 1- First Class Code


_onTappedCallback(){
     //TODO: do some work
  }
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondClass('Abc',123,_onTappedCallback)));


 Step 2- Second Class code


onPressed: (){
   widget.callback();
}




Example -

 ---------First class------------


 _onTappedCallback(){
    //TODO: do some work
}
_onCallToSecondClass(){
  Navigator.push(context, MaterialPageRoute(builder: (context) => SecondClass('Abc',123,_onTappedCallback)));
}


----------Second class----------


class SecondClass extends StatefulWidget {
  final String userName;
  final int rNumber;
  final dynamic callback;
  SecondClass(this.userName, this.rNumber, this.callback);
  @override
  _SecondClassState createState() => _SecondClassState();
}

class _SecondClassState extends State<SecondClass> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Title'),
          centerTitle: true,
          backgroundColor: AppTheme.appBarColor,
        ),
        body: FlatButton(
            onPressed: (){
              widget.callback();
         },
            child: Text('Return Your Result ${widget.userName}'))
    );
  }
}





Previous
Next Post »