Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.7 kB
1
Indexable
Never
String type = "";

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final user = FirebaseAuth.instance.currentUser;
  QuerySnapshot<Map<String, dynamic>>? orderSnap;
  String? firstname;
  String? lastname;

  void checkUser() async {
    String x = "";
    await FirebaseFirestore.instance
        .collection('users')
        .doc(user!.uid)
        .get()
        .then(
      (DocumentSnapshot documentSnapshot) {
        if (documentSnapshot.exists) {
          var data = documentSnapshot.data();
          var res = data as Map<String, dynamic>;
          if (res["type"] == "Salarié") {
            print('Salarié');
          } else if (res["type"] == "Auto entrepreneur") {
            print('Auto entrrepreneu');
          } else {
            showDialog<String>(
                context: context,
                builder: (_) => StatefulBuilder(
                      builder: (modalContext, modalSetState) => AlertDialog(
                        title: const Text('Choissisez votre type'),
                        content: const Text('Choisir votre type de user'),
                        actions: <Widget>[
                          TextButton(
                            onPressed: () {
                              setState(() {
                                x = "Salarié";
                              });
                              Navigator.pop(context, 'Cancel');
                            },
                            child: const Text('Salarié'),
                          ),
                          TextButton(
                            onPressed: () {
                              setState(() {
                                x = "Auto entrepreneur";
                              });
                              Navigator.pop(context, 'OK');
                            },
                            child: const Text('Auto-entrepreneur'),
                          ),
                        ],
                      ),
                    ));
            setState(() {
              type = x;
            });
            return FirebaseFirestore.instance
                .collection("users")
                .doc(user!.uid)
                .update({
              "type": type,
            });
          }
        } else {}
      },
    );
  }


  @override
  void initState() {
    super.initState();
    checkUser();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(....);