Reputation: 1067
I'm new to the Flutter. I'm calling a async
function to get the data from server.In my code Navigator.push()
has to be executed after the async onEdit()
function complete. But for me Navigator.push()
executed before onEdit()
completes.
code:
void onEdit()async {
value= await getJson();
print(value);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromRGBO(33, 64, 95, 1.0),
leading: Icon(Icons.chevron_left),
actions: <Widget>[
FlatButton(
onPressed: (){
onEdit();
Navigator.push(context, MaterialPageRoute(builder: (context) => new Case(value)) );
},
child: Text(
"Edit",
style: TextStyle(color: Colors.white),
))
],
),
Upvotes: 2
Views: 670
Reputation: 2711
Just call the onEdit
function with the await
keyword.
Future<void> onEdit() async {
value = await getJson();
print(value);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromRGBO(33, 64, 95, 1.0),
leading: Icon(Icons.chevron_left),
actions: <Widget>[
FlatButton(
onPressed: () async {
await onEdit();
Navigator.push(context, MaterialPageRoute(builder: (context) => new Case(value)) );
},
child: Text(
"Edit",
style: TextStyle(color: Colors.white),
)
)
],
),
);
}
Upvotes: 2