Saad Bashir
Saad Bashir

Reputation: 4519

Flutter: Redirect to another page from widget

I am trying to implement a logout page. So when the user clicks on logout button in the navigation following code is called:

Class Logout extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
      final provider = Provider.of<SignInProvider>(context, listen: true);
      Future.delayed(Duration(seconds: 5), () async {
         provider.isLoggedIn = false;
         provider.notifyListeners();
         Navigator.pushReplacement(
            context, new MaterialPageRoute(builder: (context) => LoginGate()));
      });
       return Center(child: CircularProgressIndicator());
   }
}

I get the following error:

The following assertion was thrown building MainScreen(dirty, dependencies: [_InheritedProviderScope<SelectedIndex?>, _InheritedProviderScope<SignInProvider?>], state: _MainScreenState#6a8ce):
setState() or markNeedsBuild() called during build.

I tried adding the delay hoping that would fix the issue but didn't help. Would appreciate some help on how to handle this.

Logout Button is shown using NavigationRail

const NavigationRailDestination(
  icon: Icon(Icons.logout),
  label: Text('Logout'),
),

And the Logout widget is called using following:

child: Row(
   children: [
     NavigationRailExample(),
     const VerticalDivider(thickness: 1, width: 1),
     Expanded(
        child: screenSwitch[providerSelectedIndex.selectedIndex],
     )
   ],
 ),
List<Widget> screenSwitch = [
   HomeScreen(),
   Screen1(),
   Screen2(),
   Screen3(),
   Screen4(),
   Screen5(),
   Screen6(),
   Logout(),
];

Upvotes: 1

Views: 1567

Answers (1)

eamirho3ein
eamirho3ein

Reputation: 17940

You are calling your async function in build method which is wrong. Try this:

class Logout extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: doLogOut(context),
      builder: (context, snapshot) {
        return Center(child: CircularProgressIndicator());
      },
    );
  }

  Future<void> doLogOut(BuildContext context) async {
    final provider = Provider.of<SignInProvider>(context, listen: true);
    await Future.delayed(Duration(seconds: 5), () async {
      provider.isLoggedIn = false;
      provider.notifyListeners();
      Navigator.pushReplacement(
          context, new MaterialPageRoute(builder: (context) => LoginGate()));
    });
  }
}

Upvotes: 5

Related Questions