I am new to flutter and I am trying to perform signOut() function on sidenavbar
. I got stuck in a code segment. After a couple of attempts, I got errors like:
type Future is not a subtype of type Widget
I have the following code. How should I call _logOutUser() function in my case 0: statement? Any help would be highly appreciated
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
class Homepage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
routes: {
'/login': (context) => Login(),
},
title: 'NavigationDrawer Demo',
theme: new ThemeData(
primarySwatch: Colors.red,
),
home: new HomePage(),
);
}
}
class DrawerItem {
String title;
IconData icon;
DrawerItem(this.title, this.icon);
}
class HomePage extends StatefulWidget {
HomePage({
this.auth,
this.onSignedOut,
});
final AuthImplementation auth;
final VoidCallback onSignedOut;
final drawerItems = [
new DrawerItem("Logout", Icons.exit_to_app),
];
@override
State<StatefulWidget> createState() {
return new HomePageState();
}
}
class HomePageState extends State<HomePage> {
void _logOutUser() async {
try {
await widget.auth.signOut();
widget.onSignedOut();
} catch (e) {
print(e.toString());
}
}
int _selectedDrawerIndex = 0;
_getDrawerItemWidget(int pos) {
switch (pos) {
case 0:
return HomePageState()._logOutUser();
default:
return new Text("Error");
}
}
_onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
var drawerOptions = <Widget>[];
for (var i = 0; i < widget.drawerItems.length; i++) {
var d = widget.drawerItems[i];
drawerOptions.add(
new ListTile(
leading: new Icon(d.icon),
title: new Text(d.title),
selected: i == _selectedDrawerIndex,
onTap: () => _onSelectItem(i),
)
);
}
}
}
I'm a little confused by your question, but if I'm not mistaken why don't you just do this:
switch (pos) {
case 0:
HomePageState()._logOutUser();
return Text("Logged Out");
// or if you don't want to show anything you could just return a blank Container, or
// if you wanted to navigate to login screen just use Navigator.push() and return the Container
default:
return new Text("Error");
}
The reason you are getting the error type Future<dynamic> is not a subtype of Widget
is because your Function _logOutUser()
isn't returning a Widget. Comment below if I didn't answer your question.