11import 'dart:io' ;
2-
32import 'package:cookethflow/core/utils/enums.dart' ;
43import 'package:cookethflow/core/utils/state_handler.dart' ;
54import 'package:cookethflow/core/utils/ui_helper.dart' ;
65import 'package:cookethflow/models/flow_manager.dart' ;
76import 'package:cookethflow/models/flow_node.dart' ;
87import 'package:file_selector/file_selector.dart' ;
8+ import 'package:flutter/foundation.dart' ; // For kIsWeb
99import 'package:flutter/material.dart' ;
1010import 'package:http/http.dart' as http;
1111import 'package:path_provider/path_provider.dart' ;
@@ -19,7 +19,7 @@ class SupabaseService extends StateHandler {
1919
2020 late AuthResponse _userData;
2121 bool _userDataSet = false ;
22- XFile ? _userPfp = XFile ( 'assets/Frame 271.png' ) ;
22+ XFile ? _userPfp;
2323 String ? _userName;
2424 String ? _email;
2525
@@ -38,7 +38,7 @@ class SupabaseService extends StateHandler {
3838
3939 void setUserPfp (XFile ? val) {
4040 _userPfp = val;
41- print ('Updated userPfp: ${val ?.name }' );
41+ print ('Updated userPfp: ${val ?.path }' );
4242 notifyListeners ();
4343 }
4444
@@ -56,7 +56,6 @@ class SupabaseService extends StateHandler {
5656 return text.length > 12 ? '${text .substring (0 , 12 )}...' : text;
5757 }
5858
59- // Initialize user data on service creation
6059 Future <void > _initializeUserData () async {
6160 final user = supabase.auth.currentUser;
6261 if (user != null ) {
@@ -67,9 +66,7 @@ class SupabaseService extends StateHandler {
6766
6867 Future <Map <String , dynamic >?> fetchCurrentUserName () async {
6968 final user = supabase.auth.currentUser;
70- if (user == null ) {
71- return null ;
72- }
69+ if (user == null ) return null ;
7370
7471 try {
7572 final response = await supabase
@@ -87,9 +84,7 @@ class SupabaseService extends StateHandler {
8784
8885 Future <Map <String , dynamic >?> fetchCurrentUserDetails () async {
8986 final user = supabase.auth.currentUser;
90- if (user == null ) {
91- return null ;
92- }
87+ if (user == null ) return null ;
9388
9489 try {
9590 final response =
@@ -272,7 +267,7 @@ class SupabaseService extends StateHandler {
272267 await supabase.auth.signOut ();
273268 _userName = null ;
274269 _email = null ;
275- _userPfp = XFile ( 'assets/Frame 271.png' );
270+ _userPfp = null ; // Set to null to use asset in UI
276271 _userDataSet = false ;
277272 notifyListeners ();
278273 } catch (e) {
@@ -289,7 +284,7 @@ class SupabaseService extends StateHandler {
289284 await supabase.auth.signOut ();
290285 _userName = null ;
291286 _email = null ;
292- _userPfp = XFile ( 'assets/Frame 271.png' );
287+ _userPfp = null ; // Set to null to use asset in UI
293288 _userDataSet = false ;
294289 notifyListeners ();
295290 } catch (e) {
@@ -383,12 +378,10 @@ class SupabaseService extends StateHandler {
383378 final user = supabase.auth.currentUser;
384379 if (user == null ) throw Exception ('User not authenticated' );
385380
386- // Get file extension and MIME type
387381 final extension = imageFile.name.split ('.' ).last.toLowerCase ();
388382 final mimeType = _getMimeTypeFromExtension (extension );
389383 final storagePath = '${user .id }/pfp.$extension ' ;
390384
391- // Remove existing profile picture if it exists
392385 try {
393386 await supabase.storage
394387 .from (_profileBucketName)
@@ -397,28 +390,22 @@ class SupabaseService extends StateHandler {
397390 print ('No existing profile picture to remove: $e ' );
398391 }
399392
400- // Read file bytes for web compatibility
401393 final bytes = await imageFile.readAsBytes ();
402394
403- // Upload to Supabase
404395 await supabase.storage.from (_profileBucketName).uploadBinary (
405396 storagePath,
406397 bytes,
407398 fileOptions: FileOptions (contentType: mimeType, upsert: true ),
408399 );
409400
410- // Get public URL
411401 final String publicUrl =
412402 supabase.storage.from (_profileBucketName).getPublicUrl (storagePath);
413403
414- // Update user table with new URL
415404 await supabase
416405 .from ('User' )
417406 .update ({'profile_picture_url' : publicUrl}).eq ('id' , user.id);
418407
419- // Update local state
420- setUserPfp (imageFile);
421- notifyListeners ();
408+ setUserPfp (imageFile); // Use the uploaded file directly
422409 return publicUrl;
423410 } catch (e) {
424411 print ('Error uploading profile picture: $e ' );
@@ -446,22 +433,32 @@ class SupabaseService extends StateHandler {
446433 const supportedExtensions = ['jpg' , 'jpeg' , 'png' , 'gif' , 'webp' ];
447434
448435 if (supportedExtensions.contains (fileExtension)) {
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- ));
436+ if (kIsWeb) {
437+ // Web: Use XFile.fromData for in-memory bytes
438+ final bytes = await http.get (uri).then ((res) => res.bodyBytes);
439+ setUserPfp (XFile .fromData (
440+ bytes,
441+ name: 'pfp.$fileExtension ' ,
442+ mimeType: _getMimeTypeFromExtension (fileExtension),
443+ ));
444+ } else {
445+ // Desktop: Save to temporary file
446+ final tempDir = await getTemporaryDirectory ();
447+ final tempFile = File ('${tempDir .path }/pfp.$fileExtension ' );
448+ final bytes = await http.get (uri).then ((res) => res.bodyBytes);
449+ await tempFile.writeAsBytes (bytes);
450+ setUserPfp (XFile (tempFile.path));
451+ }
455452 notifyListeners ();
456453 return ;
457454 }
458455 }
459- // Fallback to default image
460- setUserPfp (XFile (_defaultPfpPath) );
456+ // Fallback to null (UI should use Image.asset for default)
457+ setUserPfp (null );
461458 notifyListeners ();
462459 } catch (e) {
463460 print ('Error fetching profile picture: $e ' );
464- setUserPfp (XFile (_defaultPfpPath) );
461+ setUserPfp (null );
465462 notifyListeners ();
466463 }
467464 }
0 commit comments