@@ -7,6 +7,7 @@ import 'package:cookethflow/models/flow_manager.dart';
77import 'package:cookethflow/models/flow_node.dart' ;
88import 'package:file_selector/file_selector.dart' ;
99import 'package:flutter/material.dart' ;
10+ import 'package:http/http.dart' as http;
1011import 'package:path_provider/path_provider.dart' ;
1112import 'package:supabase_flutter/supabase_flutter.dart' ;
1213
@@ -27,6 +28,7 @@ class SupabaseService extends StateHandler {
2728 XFile ? get userPfp => _userPfp;
2829 String ? get userName => _userName;
2930 String ? get email => _email;
31+ String get defaultPfpPath => _defaultPfpPath;
3032
3133 void setUserData (AuthResponse user) {
3234 _userData = user;
@@ -36,6 +38,7 @@ class SupabaseService extends StateHandler {
3638
3739 void setUserPfp (XFile ? val) {
3840 _userPfp = val;
41+ print ('Updated userPfp: ${val ?.name }' );
3942 notifyListeners ();
4043 }
4144
@@ -379,28 +382,41 @@ class SupabaseService extends StateHandler {
379382 try {
380383 final user = supabase.auth.currentUser;
381384 if (user == null ) throw Exception ('User not authenticated' );
382- final extension = imageFile.path.split ('.' ).last.toLowerCase ();
385+
386+ // Get file extension and MIME type
387+ final extension = imageFile.name.split ('.' ).last.toLowerCase ();
383388 final mimeType = _getMimeTypeFromExtension (extension );
384389 final storagePath = '${user .id }/pfp.$extension ' ;
385390
391+ // Remove existing profile picture if it exists
386392 try {
387393 await supabase.storage
388394 .from (_profileBucketName)
389395 .remove (['${user .id }/pfp' ]);
390396 } catch (e) {
391- print ('No existing profile picture to remove' );
397+ print ('No existing profile picture to remove: $ e ' );
392398 }
393399
394- await supabase.storage.from (_profileBucketName).upload (
400+ // Read file bytes for web compatibility
401+ final bytes = await imageFile.readAsBytes ();
402+
403+ // Upload to Supabase
404+ await supabase.storage.from (_profileBucketName).uploadBinary (
395405 storagePath,
396- File (imageFile.path) ,
406+ bytes ,
397407 fileOptions: FileOptions (contentType: mimeType, upsert: true ),
398408 );
409+
410+ // Get public URL
399411 final String publicUrl =
400412 supabase.storage.from (_profileBucketName).getPublicUrl (storagePath);
413+
414+ // Update user table with new URL
401415 await supabase
402416 .from ('User' )
403417 .update ({'profile_picture_url' : publicUrl}).eq ('id' , user.id);
418+
419+ // Update local state
404420 setUserPfp (imageFile);
405421 notifyListeners ();
406422 return publicUrl;
@@ -430,25 +446,23 @@ class SupabaseService extends StateHandler {
430446 const supportedExtensions = ['jpg' , 'jpeg' , 'png' , 'gif' , 'webp' ];
431447
432448 if (supportedExtensions.contains (fileExtension)) {
433- try {
434- final response = await supabase.storage
435- .from (_profileBucketName)
436- .download ('${user .id }/pfp.$fileExtension ' );
437- final tempDir = await getTemporaryDirectory ();
438- final file = File ('${tempDir .path }/pfp_${user .id }.$fileExtension ' );
439- await file.writeAsBytes (response);
440- setUserPfp (XFile (file.path));
441- notifyListeners ();
442- return ;
443- } catch (e) {
444- print ('Error downloading profile picture: $e ' );
445- }
449+ // For web, store the URL directly instead of downloading
450+ setUserPfp (XFile .fromData (
451+ await http.get (Uri .parse (pfpUrl)).then ((res) => res.bodyBytes),
452+ name: 'pfp.$fileExtension ' ,
453+ mimeType: _getMimeTypeFromExtension (fileExtension),
454+ ));
455+ notifyListeners ();
456+ return ;
446457 }
447458 }
459+ // Fallback to default image
448460 setUserPfp (XFile (_defaultPfpPath));
461+ notifyListeners ();
449462 } catch (e) {
450463 print ('Error fetching profile picture: $e ' );
451464 setUserPfp (XFile (_defaultPfpPath));
465+ notifyListeners ();
452466 }
453467 }
454468
0 commit comments