Reputation: 59
Navigator.push is not working with setState
.
Once I removed the setState in the retry()
function then it works but I want to use the setState inside the retry
function.
import 'package:covid_19/screens/home.dart';
import 'package:covid_19/viewmodel/home_view_model.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyAppError());
class MyAppError extends StatefulWidget {
@override
_MyAppErrorState createState() => _MyAppErrorState();
}
class _MyAppErrorState extends State<MyAppError> {
bool _loading = false;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return _loading ? CircularProgressIndicator() : MaterialApp(
home: Scaffold(
key: _scaffoldKey,
body: Builder(
builder:(context)=>
SafeArea(
child: Container(
margin: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"No internet found. Check your connection",
textAlign: TextAlign.center,
),
FlatButton(
child: Text('Retry'),
onPressed: retry,
)
],
),
),
),
),
)
);
}
Future<void> retry() async{
setState(() {
_loading = true;
});
print('retrying...');
HomeViewModel homeViewModel = HomeViewModel();
homeViewModel.onAppStart().then((_){
print('pushing');
Navigator.pushReplacement(_scaffoldKey.currentContext, MaterialPageRoute(builder: (context)=> Home(data : homeViewModel.data)));
}).catchError((e){
setState(() {
_loading = false;
});
});
}
}
logs when the retry button clicked without setState
:
retrying...
pushing...
and navigate to Home()
logs when the retry button clicked with setState
:
retrying...
pushing...
and nothing happens
Upvotes: 2
Views: 1455
Reputation: 954
It has a logical error. when the _loading is true, there is no scaffold and you are using Scaffold key for navigating.
try this.
import 'package:covid_19/screens/home.dart';
import 'package:covid_19/viewmodel/home_view_model.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyAppError());
class MyAppError extends StatefulWidget {
@override
_MyAppErrorState createState() => _MyAppErrorState();
}
class _MyAppErrorState extends State<MyAppError> {
bool _loading = false;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffoldKey,
body:_loading ? CircularProgressIndicator() : Builder(
builder:(context)=>
SafeArea(
child: Container(
margin: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"No internet found. Check your connection",
textAlign: TextAlign.center,
),
FlatButton(
child: Text('Retry'),
onPressed: retry,
)
],
),
),
),
),
)
);
}
Future<void> retry() async{
setState(() {
_loading = true;
});
print('retrying...');
HomeViewModel homeViewModel = HomeViewModel();
homeViewModel.onAppStart().then((_){
print('pushing');
Navigator.pushReplacement(_scaffoldKey.currentContext, MaterialPageRoute(builder: (context)=> Home(data : homeViewModel.data)));
}).catchError((e){
setState(() {
_loading = false;
});
});
}
}
Upvotes: 1