import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:diacritic/diacritic.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:prosappco/src/authentication/authentication_repository.dart'; import 'package:prosappco/src/components/pop_appbar.dart'; final CollectionReference professionsCollection = FirebaseFirestore.instance.collection('professions'); Future> getProfessions() async { try { DocumentSnapshot profession = await professionsCollection.doc('professions').get(); Map data = profession.data() as Map; var professionsList = (data['professions'] as List) .map((e) => e.toString()) .toList(); return professionsList; } catch (e) { print('xd $e'); } return []; } class ProfessionScreen extends StatefulWidget { const ProfessionScreen({super.key}); @override State createState() => _ProfessionScreenState(); } class _ProfessionScreenState extends State { List? filteredProfessions; TextEditingController searchController = TextEditingController(); final User? user = FirebaseAuth.instance.currentUser; List? _professions; final uid = AuthenticationRepository.instance.getCurrentUserUid(); @override void initState() { super.initState(); searchController.addListener(() { setState(() { if (_professions != null) { if (searchController.text.isEmpty) { filteredProfessions = _professions!; } else { filteredProfessions = _professions! .where((profession) => removeDiacritics(profession) .toLowerCase() .contains( removeDiacritics(searchController.text.toLowerCase()))) .toList(); } } }); }); if (_professions == null) { getProfessions().then((List element) => setState(() { _professions = element; filteredProfessions = element; })); } } Future updateProfession(String profession) async { try { await FirebaseFirestore.instance .collection('users') .doc(uid) .update({'profesion': profession}); print('Profesion actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance .collection('users') .doc(uid) .set({'profesion': profession}); } catch (e) { print('Error al agregar la profesion: $e'); } print('Error al actualizar la profesion: $e'); } } @override Widget build(BuildContext context) { if (filteredProfessions == null) { return const Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation(Color(0xFF2BA4EC)), ), ); } var professions = filteredProfessions!; return SafeArea( child: Scaffold( appBar: PopAppbar( onPressed: () { Navigator.pop(context); }, label: 'Seleccione su profesión', ), body: Column( children: [ Padding( padding: EdgeInsets.only(left: 10, right: 10, top: 10), child: TextField( controller: searchController, decoration: InputDecoration( hintText: 'Buscar su profesión', prefixIcon: Icon(Icons.assignment_ind_rounded), ), ), ), Expanded( child: ListView.builder( itemCount: professions.length, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text( professions[index], style: const TextStyle( fontSize: 18.0, color: Colors.black, ), ), onTap: () { updateProfession(professions[index]); Navigator.pop(context, professions[index]); }, ); }, ), ), ], ), )); } }