I have this code on Flutter and I found a big problem: I need to send a parameter (isAdmin) to other widget in other page. I know that I have to send this by parameter in the widget, but the issue is the parameter isAdmin needs to access to the database to achieve his real value.

On summary, the widget loads before the access to the database had ended, so the real value of isAdmin is not sent. How can I do that?

class Inicio_User extends StatefulWidget {
  Inicio_User({Key? key}) : super(key: key);
  @override
  State<Inicio_User> createState() => _InicioUserState();
}

class _InicioUserState extends State<Inicio_User> {
  int _paginaActual = 0;
  bool isAdmin = false;
  List<Widget> _paginas = [
    widget_productos(isAdmin),
    widget_restaurantes(),
    widget_perfil()
  ];

  @override
  Widget build(BuildContext context) {
    final User? usuario = FirebaseAuth.instance.currentUser;
    String? email = "";
    String? uid = "";
    String? nombre = "";
    final firestoreInstance = FirebaseFirestore.instance;

    if (usuario != null) {
      email = usuario.email!;
      uid = usuario.uid;
      firestoreInstance
          .collection("Usuario")
          .where("email", isEqualTo: email)
          .get()
          .then((value) {
        value.docs.forEach((result) {
          // HAGO UPDATE DEL UID EN CASO DE QUE SEA NULL (PORQUE SE ACABA DE REGISTRAR)
          if (result.get('UID') == 'null') {
            print("\nMODIFICANDO UID PORQUE ERA NULL\n");
            firestoreInstance
                .collection("Usuario")
                .doc(result.id)
                .update({"UID": uid}).then((_) {
              print("success!");
            });
          }
          isAdmin = result.get("isAdmin");
          _paginas[0] = widget_productos(isAdmin);
        });
      });
    }
    return Scaffold(
            body: _paginas[_paginaActual],
            appBar: AppBar(
              automaticallyImplyLeading: false,
              title: TextButton.icon(
                onPressed: () {
                  _salir(context);
                },
                label: Text('BIENVENIDO A CELIPAL - TU AMIGO CELIACO',
                    style: TextStyle(
                        fontSize: 15,
                        color: Color.fromARGB(255, 255, 255, 255))),
                icon: Icon(
                  Icons.logout,
                  color: Colors.white70,
                ),
              ),
            ),
            bottomNavigationBar: BottomNavigationBar(
              /*
        CurrentIndex es para que por defecto se muestre el BottomNavigationBarItem numero 0 del array (el primero, en mi caso seria la listaproductos.)*/
              currentIndex: _paginaActual,
              onTap: (index) => {
                setState(() => {_paginaActual = index})
              },
              items: [
                BottomNavigationBarItem(
                    icon: Icon(Icons.home), label: "Productos"),
                BottomNavigationBarItem(
                    icon: Icon(Icons.deck_outlined), label: "Restaurantes"),
                BottomNavigationBarItem(
                    icon: Icon(Icons.account_box_rounded), label: "Mi perfil")
              ],
            ),
          );
  }

  void _salir(BuildContext context) async {
    await FirebaseAuth.instance.signOut();
    Navigator.pop(context);
  }

}


Solution 1: Manal

void whois() async{
final User? usuario = FirebaseAuth.instance.currentUser;
if (usuario != null) {
email = usuario.email!;
uid = usuario.uid;
await firestoreInstance
.collection("Usuario")
.where("email", isEqualTo: email)
.get()
.then((value) {
value.docs.forEach((result) {
// HAGO UPDATE DEL UID EN CASO DE QUE SEA NULL (PORQUE SE ACABA DE REGISTRAR)
if (result.get('UID') == 'null') {
print("\nMODIFICANDO UID PORQUE ERA NULL\n");
firestoreInstance
.collection("Usuario")
.doc(result.id)
.update({"UID": uid}).then((_) {
print("success!");
});
}
setState(() {
isAdmin = result.get("isAdmin");
_paginas[0] = widget_productos(isAdmin);
});

  });
});

} }