Home > Enterprise >  show pages based on user role on flutter with firestore
show pages based on user role on flutter with firestore

Time:11-15

am developing a flutter application with firestore. in firestore I have a field called role and I managed to access it in my code. The role is printed correctly in the console.But I need help in displaying differnt page for each user.

`

class _UserMangmentState extends State<UserMangment> {
  String role = "";
  @override
  void initState() {
    getRole();
    super.initState();
  }

  Future getRole() async {
    String id = FirebaseAuth.instance.currentUser!.uid.toString();
    FirebaseFirestore.instance
        .collection('users')
        .doc(id)
        .get()
        .then((DocumentSnapshot doc) {
      role = doc.get('role');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
          builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Container();
        } else {
          getRole();
          if (role == "vendor") {
            return VendorInformation();
          } else if (role == "planner") {
            return PlannerPage();
          } else {
            return HomePage();
          }
        }
      }),
    );
  }
}

`

CodePudding user response:

Your approach isn't optimal. You can try this. Hope it helps

class UserManager extends StatelessWidget {
  UserManager({Key? key}) : super(key: key);

  final firebaseAuthUser = FirebaseAuth.instance.currentUser;
  
  Future<DocumentSnapshot<Map<String, dynamic>>?> getRole() async {
    final id = firebaseAuthUser?.uid;
    if (id == null) {
      return null;
    }
    return FirebaseFirestore.instance.collection('users').doc(id).get();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<DocumentSnapshot<Map<String, dynamic>>?>(
          future: getRole(),
          builder: (BuildContext context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting && snapshot.data == null) {
              return const Center(child: CircularProgressIndicator());
            }

            if (snapshot.connectionState == ConnectionState.done && snapshot.data == null) {
              return const Center(child: Text('No data'));
            }

            final role = snapshot.data?.data()?['role'];

            if (role == null) {
              return const Center(child: Text('No role found'));
            }

            switch (role) {
              case 'vendor':
                return VendorInformation();
              case 'planner':
                return PlannerPage();
              default:
                return HomePage();
            }
          }),
    );
  }
}

CodePudding user response:

You are trying to changing the role value without using setState the value of role never changed

class _UserMangmentState extends State<UserMangment> {
  String role = "";
  @override
  void initState() {
    getRole();
    super.initState();
  }

  Future getRole() async {
    String id = FirebaseAuth.instance.currentUser!.uid.toString();
    FirebaseFirestore.instance
        .collection('users')
        .doc(id)
        .get()
        .then((DocumentSnapshot doc) {
         setState(){
         role = doc.get('role');
        };

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
          builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          getRole();
          if (role == "vendor") {
            return VendorInformation();
          } else if (role == "planner") {
            return PlannerPage();
          } else {
            return HomePage();
          }
        }else{
return Container();}
      }),
    );
  }
}
  • Related