I successfully did the register and login page. Now I want to have when a specific user which has the role of admin logs in that it will navigate him to admin home page, if it has a user role it will navigate them to homepage. How can I do this? i did this but it keeps returning me to admin page even though i logged in with a user who has the role : user
and i get this error
The method ‘[]’ was called on null.
Receiver: null
Tried calling:
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
final AuthService _auth = AuthService();
int _currentIndex = 0;
PageController _pageController;
@override
void initState() {
super.initState();
_pageController = PageController();
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
Future<FirebaseUser> currentUser() async {
FirebaseAuth _firebaseInstance = FirebaseAuth.instance;
return _firebaseInstance.currentUser();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: currentUser(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return StreamBuilder(
stream: Firestore.instance
.collection('users')
.document()
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data['role'] == 'admin') {
return AdminHomePage();
} else {
return Home();
}
}
);}
return Scaffold(
resizeToAvoidBottomPadding: true,
drawer: Container(
height: 200,
child: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 10 , right: 13),
child: ListTile(
title: Text('Login'),
onTap: (){
Navigator.push(context, MaterialPageRoute(
builder:(context){
return LoginPage();
},));},
),
),
ListTile(
title: Text('Logout'),
onTap: ()async{
await _auth.signOut();
},
),
ListTile(
title: Text('Register'),
onTap: (){
Navigator.push(context, MaterialPageRoute(
builder:(context){
return RegisterPage();
}));})],
),
),
),
appBar: AppBar(
elevation: 3,
backgroundColor: Color(0xff828EFB),
title: Text(
'Cinema Court',
style: GoogleFonts.poppins(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.white
),),
titleSpacing: 6,
centerTitle: true,
toolbarHeight: 70,
),
body: SizedBox.expand(
child: PageView(
controller: _pageController,
onPageChanged: (index) {
setState(() => _currentIndex = index);
},
children: <Widget>[
TopTabBar(),
Profile(),
Foods('food')
],
),
),
bottomNavigationBar: BottomNavyBar(
backgroundColor: Color(0xff828EFB),
selectedIndex: _currentIndex,
onItemSelected: (index) {
setState(() => _currentIndex = index);
_pageController.jumpToPage(index);
},
items: [
BottomNavyBarItem(
icon: Icon(Icons.movie, color: Colors.black),
title: Text(
'Movies',
style: GoogleFonts.poppins(fontWeight: FontWeight.bold)),
activeColor: Colors.white,
inactiveColor: Colors.black,
),
BottomNavyBarItem(
icon: Icon(Icons.people, color: Colors.black),
title: Text(
'Profile',
style: GoogleFonts.poppins(fontWeight: FontWeight.bold)),
activeColor: Colors.white,
inactiveColor: Colors.black
),
BottomNavyBarItem(
icon: Icon(Icons.fastfood , color: Colors.black),
title: Text(
'FoodMenu',
style: GoogleFonts.poppins(fontWeight: FontWeight.bold)),
activeColor: Colors.white,
inactiveColor: Colors.black
),
],
)
);},
);
}}