From eda3a928cd3f841d9d8f27cdca03bef27b64f2de Mon Sep 17 00:00:00 2001 From: Bart Ribbers Date: Fri, 4 Apr 2025 13:22:09 +0200 Subject: [PATCH] fix: only accept nullable strings when building items for an itemlist The value is put directly in a TextEditingController so it needs to be a string, any other value has to be parsed to a string by the user first. --- example/lib/utils/example_profile_data.dart | 4 +- lib/src/models/user.dart | 4 +- .../widgets/item_builder/item_builder.dart | 6 +-- lib/src/widgets/item_builder/item_list.dart | 54 ++++++++----------- test/test_classes/test_profile_data.dart | 4 +- 5 files changed, 31 insertions(+), 41 deletions(-) diff --git a/example/lib/utils/example_profile_data.dart b/example/lib/utils/example_profile_data.dart index 8c569ca..bf0f5cb 100644 --- a/example/lib/utils/example_profile_data.dart +++ b/example/lib/utils/example_profile_data.dart @@ -17,7 +17,7 @@ class ExampleProfileData extends ProfileData { String? remarks; @override - Map mapWidget( + Map mapWidget( VoidCallback update, BuildContext context, ) { @@ -38,7 +38,7 @@ class ExampleProfileData extends ProfileData { } @override - Map toMap() { + Map toMap() { return {'email': email, 'about': about, 'remarks': remarks}; } diff --git a/lib/src/models/user.dart b/lib/src/models/user.dart index b2eee7b..ae3cc3f 100644 --- a/lib/src/models/user.dart +++ b/lib/src/models/user.dart @@ -76,9 +76,9 @@ abstract class ProfileData { ProfileData fromMap(Map data); - Map toMap(); + Map toMap(); - Map mapWidget(VoidCallback update, BuildContext context); + Map mapWidget(VoidCallback update, BuildContext context); ProfileData create(); } diff --git a/lib/src/widgets/item_builder/item_builder.dart b/lib/src/widgets/item_builder/item_builder.dart index 0f8b969..8b1da7d 100644 --- a/lib/src/widgets/item_builder/item_builder.dart +++ b/lib/src/widgets/item_builder/item_builder.dart @@ -21,8 +21,8 @@ class ItemBuilder { String key, String? value, Widget? widget, - Function(String) updateItem, - Function(String?) saveItem, + void Function(String) updateItem, + void Function(String?) saveItem, ) { if (widget == null) { var controller = TextEditingController( @@ -55,7 +55,7 @@ class ItemBuilder { Widget buildPassword( String key, TextEditingController controller, - Function(String?) onChanged, + void Function(String?) onChanged, String? Function(String?) validator, ) { var inputDecoration = diff --git a/lib/src/widgets/item_builder/item_list.dart b/lib/src/widgets/item_builder/item_list.dart index 73e5339..6df69ac 100644 --- a/lib/src/widgets/item_builder/item_list.dart +++ b/lib/src/widgets/item_builder/item_list.dart @@ -16,49 +16,39 @@ class ItemList { this.itemBuilder, this.itemBuilderOptions, }) { - for (var item in items.entries) { - widgets.addAll({ - item.key: itemBuilder == null - ? builder.build( - item.key, - item.value, - typeMap[item.key], - (value) { - saveProfile(); - }, - (value) { - updateProfile(item.key, value); - }, - ) - : itemBuilder!.build( - item.key, - item.value, - typeMap[item.key], - (value) { - saveProfile(); - }, - (value) { - updateProfile(item.key, value); - }, - ), - }); - } + var itemBuilder = this.itemBuilder ?? builder; + + widgets = { + for (var item in items.entries) ...{ + item.key: itemBuilder.build( + item.key, + item.value, + typeMap[item.key], + (value) { + saveProfile(); + }, + (value) { + updateProfile(item.key, value); + }, + ), + }, + }; } /// Gets the map of item keys and their corresponding widgets. Map getItemList() => widgets; /// Map containing item keys and their values. - final Map items; + final Map items; /// Map containing item keys and their types. - final Map typeMap; + final Map typeMap; /// Function to update the profile with a specific item's value. - final Function(String, String?) updateProfile; + final void Function(String, String?) updateProfile; /// Function to save the profile after an item value is updated. - final Function() saveProfile; + final void Function() saveProfile; /// Builder for custom item widgets. final ItemBuilder? itemBuilder; @@ -70,7 +60,7 @@ class ItemList { final GlobalKey formKey; /// Map containing item keys and their corresponding widgets. - Map widgets = {}; + late final Map widgets; /// `builder` is an instance of `ItemBuilder` which is used /// to build the items in the list. diff --git a/test/test_classes/test_profile_data.dart b/test/test_classes/test_profile_data.dart index 9be9280..cdf96c5 100644 --- a/test/test_classes/test_profile_data.dart +++ b/test/test_classes/test_profile_data.dart @@ -13,7 +13,7 @@ class TestProfileData extends ProfileData { String? email; @override - Map mapWidget( + Map mapWidget( VoidCallback update, BuildContext context, ) => @@ -27,7 +27,7 @@ class TestProfileData extends ProfileData { ); @override - Map toMap() => { + Map toMap() => { 'email': email, };