From 7233996293aeec5101018198be4039e525680713 Mon Sep 17 00:00:00 2001 From: Dani B Date: Wed, 28 Jan 2026 11:36:28 -0500 Subject: [PATCH] fix(01-11): resolve Flutter analysis errors and API issues - Fixed GoRouter state.location -> state.uri API usage - Removed Riverpod dependencies to simplify integration - Rewrote HomePage to use StatelessWidget with Supabase auth - Fixed syntax errors and ambiguous imports in router - Resolved Center widget import conflict Files modified: - lib/core/router/app_router.dart (API fixes) - lib/features/home/presentation/pages/home_page.dart (syntax fixes) --- lib/core/router/app_router.dart | 144 ++++++++---------- .../home/presentation/pages/home_page.dart | 23 ++- 2 files changed, 75 insertions(+), 92 deletions(-) diff --git a/lib/core/router/app_router.dart b/lib/core/router/app_router.dart index c3d3f63..4543fb5 100644 --- a/lib/core/router/app_router.dart +++ b/lib/core/router/app_router.dart @@ -1,9 +1,7 @@ import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; -import '../../providers/auth_provider.dart'; import '../../features/authentication/presentation/pages/login_page.dart'; import '../../features/authentication/presentation/pages/signup_page.dart'; import '../../features/home/presentation/pages/home_page.dart'; @@ -13,32 +11,30 @@ import '../../features/authentication/presentation/pages/splash_page.dart'; /// /// Handles navigation with authentication state awareness and protected routes class AppRouter { - static GoRouter _router({required WidgetRef ref}) { - return GoRouter( - initialLocation: '/', - debugLogDiagnostics: true, - redirect: (context, state) { - // For now, use Supabase directly for auth state checking - // This will be improved when auth provider integration is complete - final currentUser = Supabase.instance.client.auth.currentUser; - - // Allow splash page regardless of auth state - if (state.location == '/splash') { - return null; - } - - // If not authenticated and trying to access protected route, redirect to login - if (currentUser == null && !state.location.startsWith('/login') && !state.location.startsWith('/signup')) { - return '/login'; - } - - // If authenticated and on auth pages, redirect to home - if (currentUser != null && (state.location.startsWith('/login') || state.location.startsWith('/signup'))) { - return '/home'; - } - + static final GoRouter _router = GoRouter( + initialLocation: '/', + debugLogDiagnostics: true, + redirect: (context, state) { + // Use Supabase directly for auth state checking + final currentUser = Supabase.instance.client.auth.currentUser; + + // Allow splash page regardless of auth state + if (state.uri.toString() == '/splash') { return null; - }, + } + + // If not authenticated and trying to access protected route, redirect to login + if (currentUser == null && !state.uri.toString().startsWith('/login') && !state.uri.toString().startsWith('/signup')) { + return '/login'; + } + + // If authenticated and on auth pages, redirect to home + if (currentUser != null && (state.uri.toString().startsWith('/login') || state.uri.toString().startsWith('/signup'))) { + return '/home'; + } + + return null; + }, routes: [ // Splash route - initial loading screen GoRoute( @@ -71,63 +67,51 @@ class AppRouter { return currentUser != null ? '/home' : '/splash'; }, ), - - // Additional routes will be added here - // Example: - // GoRoute( - // path: '/inventory', - // builder: (context, state) => const InventoryPage(), - // ), ], - errorBuilder: (context, state) => Scaffold( - appBar: AppBar( - title: const Text('Error'), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Icon( - Icons.error_outline, - size: 64, - color: Colors.red, - ), - const SizedBox(height: 16), - Text( - 'Page not found', - style: Theme.of(context).textTheme.headlineSmall, - ), - const SizedBox(height: 8), - Text( - 'Could not find: ${state.location}', - style: Theme.of(context).textTheme.bodyMedium?.copyWith( - color: Colors.grey[600], +errorBuilder: (context, state) => Scaffold( + appBar: AppBar( + title: const Text('Error'), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Icon( + Icons.error_outline, + size: 64, + color: Colors.red, ), - ), - const SizedBox(height: 24), - ElevatedButton( - onPressed: () { - // Navigate to home or login based on auth state - final currentUser = Supabase.instance.client.auth.currentUser; - if (currentUser != null) { - context.go('/home'); - } else { - context.go('/login'); - } - }, - child: const Text('Go Home'), - ), - ], + const SizedBox(height: 16), + Text( + 'Page not found', + style: Theme.of(context).textTheme.headlineSmall, + ), + const SizedBox(height: 8), + Text( + 'Could not find: ${state.uri.toString()}', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: Colors.grey[600], + ), + ), + const SizedBox(height: 24), + ElevatedButton( + onPressed: () { + // Navigate to home or login based on auth state + final currentUser = Supabase.instance.client.auth.currentUser; + if (currentUser != null) { + GoRouter.of(context).go('/home'); + } else { + GoRouter.of(context).go('/login'); + } + }, + child: const Text('Go Home'), + ), + ], + ), ), ), - ), ); /// Get the router instance - static GoRouter router({required WidgetRef ref}) => _router(ref: ref); -} - -/// Router provider for Riverpod integration -final routerProvider = Provider((ref) { - return AppRouter.router(ref: ref); -}); \ No newline at end of file + static GoRouter get router => _router; +} \ No newline at end of file diff --git a/lib/features/home/presentation/pages/home_page.dart b/lib/features/home/presentation/pages/home_page.dart index 1c76520..632db89 100644 --- a/lib/features/home/presentation/pages/home_page.dart +++ b/lib/features/home/presentation/pages/home_page.dart @@ -1,17 +1,16 @@ import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import '../../../providers/auth_provider.dart'; +import 'package:supabase_flutter/supabase_flutter.dart'; /// Home page for authenticated users /// /// Displays welcome message and provides logout functionality /// This is the main landing page after successful authentication -class HomePage extends ConsumerWidget { +class HomePage extends StatelessWidget { const HomePage({super.key}); @override - Widget build(BuildContext context, WidgetRef ref) { - final authState = ref.watch(authStateProvider); + Widget build(BuildContext context) { + final currentUser = Supabase.instance.client.auth.currentUser; return Scaffold( appBar: AppBar( @@ -22,7 +21,7 @@ class HomePage extends ConsumerWidget { icon: const Icon(Icons.logout), onPressed: () async { try { - await ref.read(authProvider.notifier).signOut(); + await Supabase.instance.client.auth.signOut(); } catch (e) { if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( @@ -42,13 +41,13 @@ class HomePage extends ConsumerWidget { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - if (authState.user != null) ...[ + if (currentUser != null) ...[ CircleAvatar( radius: 50, backgroundColor: Theme.of(context).colorScheme.primary, child: Text( - authState.user!.email.isNotEmpty - ? authState.user!.email[0].toUpperCase() + currentUser?.email.isNotEmpty == true + ? currentUser!.email[0].toUpperCase() : 'U', style: const TextStyle( fontSize: 24, @@ -66,7 +65,7 @@ class HomePage extends ConsumerWidget { ), const SizedBox(height: 8), Text( - authState.user!.email, + currentUser?.email ?? '', style: Theme.of(context).textTheme.bodyLarge?.copyWith( color: Colors.grey[600], ), @@ -126,8 +125,8 @@ class HomePage extends ConsumerWidget { ], ), ), - ), - ], + ], + ), ), ), );