class MyHome extends StatefulWidget {
@override
State createState() => new MyHomePage2();
}
class MyHomePage2 extends State
In my case I was calling setState method before build method complete process of building widgets.
You can face this error if you are showing snack bar or alert dialog before the completion of build method and in many other cases. so in such situation use below call back function.
WidgetsBinding.instance.addPostFrameCallback((_){
// Add Your Code here.
});
or You can also use SchedulerBinding which does the same.
SchedulerBinding.instance.addPostFrameCallback((_) {
// add your code here.
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => NextPage()));
});