79571026

Date: 2025-04-12 23:11:51
Score: 1.5
Natty:
Report link

The main issue is that users (customers or barbers) don't always see the correct screen after logging in, likely because the isServiceProvider field isn't retrieved or set correctly in Firestore. Additionally, data handling in the UserModel and navigation logic can cause incorrect screens or rendering errors. Here's how to fix it:

  1. Fix UserModel to validate iServiceProvider: Avoid default values ​​that can cause misclassification of user types.
class UserModel {
  final String uid;
  final String name;
  final String fullName;
  final String email;
  final String? phoneNumber;
  final bool isServiceProvider;

  UserModel({
    required this.uid,
    required this.name,
    required this.fullName,
    required this.email,
    this.phoneNumber,
    required this.isServiceProvider,
  });

  Map<String, dynamic> toMap() {
    return {
      'uid': uid,
      'name': name,
      'fullName': fullName,
      'email': email,
      'phoneNumber': phoneNumber,
      'isServiceProvider': isServiceProvider,
    };
  }

  factory UserModel.fromMap(Map<String, dynamic> map) {
    if (!map.containsKey('isServiceProvider')) {
      throw Exception('isServiceProvider is missing');
    }
    return UserModel(
      uid: map['uid'] as String? ?? '',
      name: map['name'] as String? ?? map['fullName'] as String? ?? '',
      fullName: map['fullName'] as String? ?? map['name'] as String? ?? '',
      email: map['email'] as String? ?? '',
      phoneNumber: map['phoneNumber'] as String?,
      isServiceProvider: map['isServiceProvider'] as bool,
    );
  }
}
  1. Improve login logic: Ensure navigation is based on a valid UserModel and handles errors.
void _getUserDataAndNavigate(String uid, BuildContext context) async {
  try {
    final firestoreService = FirestoreService();
    final userData = await firestoreService.getUserById(uid);

    if (userData == null) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Error: No se encontraron datos de usuario')),
      );
      return;
    }

    if (!context.mounted) return;

    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (context) => userData.isServiceProvider
            ? BarberHomePage(userData: userData)
            : CustomerHomePage(userData: userData),
      ),
    );
  } catch (e) {
    if (context.mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: $e')),
      );
    }
  }
}
  1. Fix HomePage to avoid unnecessary reloads: Use initial data and handle loading status correctly.
class HomePage extends StatefulWidget {
  final UserModel userData;
  const HomePage({super.key, required this.userData});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _isLoading = false;
  late UserModel _userData;

  @override
  void initState() {
    super.initState();
    _userData = widget.userData;
  }

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      return const Scaffold(body: Center(child: CircularProgressIndicator()));
    }

    return _userData.isServiceProvider
        ? BarberHomePage(userData: _userData)
        : CustomerHomePage(userData: _userData);
  }
}
  1. Validate Firestore: Make sure all user documents in Firestore have an isServiceProvider field (true for barbers, false for clients). If it's missing, update the documents:
await FirebaseFirestore.instance.collection('users').doc(uid).update({
  'isServiceProvider': false, // O true para barberos
});

Next steps:

Reasons:
  • RegEx Blacklisted phrase (2): encontraron
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Santiago Galo