From 1ea41fc939f153aead043ffca629d9b2b56f7e7d Mon Sep 17 00:00:00 2001 From: Ben Corman Date: Mon, 30 Sep 2024 15:56:42 -0700 Subject: [PATCH 1/2] Fix metadata field value persistence in SupaEmailAuth widget Implement == operator and hashCode getter in MetaDataField class Update _SupaEmailAuthState to use MetaDataField.key as map key Modify related methods to work with new map structure Ensure consistent hashing and correct retrieval of TextEditingControllers Resolve issue of losing metadata field values during widget lifecycle This change prevents the loss of user input in custom metadata fields by ensuring proper object equality and consistent map key usage. --- lib/src/components/supa_email_auth.dart | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/src/components/supa_email_auth.dart b/lib/src/components/supa_email_auth.dart index 8574f24..cfd72b9 100644 --- a/lib/src/components/supa_email_auth.dart +++ b/lib/src/components/supa_email_auth.dart @@ -36,6 +36,16 @@ class MetaDataField { this.validator, this.prefixIcon, }); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is MetaDataField && + runtimeType == other.runtimeType && + key == other.key; + + @override + int get hashCode => key.hashCode; } /// {@template supa_email_auth} @@ -125,7 +135,7 @@ class _SupaEmailAuthState extends State { final _formKey = GlobalKey(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); - late final Map _metadataControllers; + late final Map _metadataControllers; bool _isLoading = false; @@ -142,7 +152,8 @@ class _SupaEmailAuthState extends State { void initState() { super.initState(); _metadataControllers = Map.fromEntries((widget.metadataFields ?? []).map( - (metadataField) => MapEntry(metadataField, TextEditingController()))); + (metadataField) => + MapEntry(metadataField.key, TextEditingController()))); } @override @@ -225,7 +236,7 @@ class _SupaEmailAuthState extends State { ...widget.metadataFields! .map((metadataField) => [ TextFormField( - controller: _metadataControllers[metadataField], + controller: _metadataControllers[metadataField.key], textInputAction: widget.metadataFields!.last == metadataField ? TextInputAction.done @@ -423,8 +434,7 @@ class _SupaEmailAuthState extends State { Map _resolveMetadataFieldsData() { return widget.metadataFields != null ? _metadataControllers.map( - (metaDataField, controller) => - MapEntry(metaDataField.key, controller.text)) + (key, controller) => MapEntry(key, controller.text)) : {}; } } From 4b3857e523e6d3d18ba94374d108d4de3bec91e6 Mon Sep 17 00:00:00 2001 From: Ben Corman Date: Mon, 30 Sep 2024 17:38:06 -0700 Subject: [PATCH 2/2] Remove unecessary equality operator override and hashcode getter. --- lib/src/components/supa_email_auth.dart | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/src/components/supa_email_auth.dart b/lib/src/components/supa_email_auth.dart index cfd72b9..82592c0 100644 --- a/lib/src/components/supa_email_auth.dart +++ b/lib/src/components/supa_email_auth.dart @@ -36,16 +36,6 @@ class MetaDataField { this.validator, this.prefixIcon, }); - - @override - bool operator ==(Object other) => - identical(this, other) || - other is MetaDataField && - runtimeType == other.runtimeType && - key == other.key; - - @override - int get hashCode => key.hashCode; } /// {@template supa_email_auth}