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.
This commit is contained in:
Bart Ribbers 2025-04-04 13:22:09 +02:00
parent 3c95ad2c3f
commit eda3a928cd
5 changed files with 31 additions and 41 deletions

View file

@ -17,7 +17,7 @@ class ExampleProfileData extends ProfileData {
String? remarks; String? remarks;
@override @override
Map<String, dynamic> mapWidget( Map<String, Widget?> mapWidget(
VoidCallback update, VoidCallback update,
BuildContext context, BuildContext context,
) { ) {
@ -38,7 +38,7 @@ class ExampleProfileData extends ProfileData {
} }
@override @override
Map<String, dynamic> toMap() { Map<String, String?> toMap() {
return {'email': email, 'about': about, 'remarks': remarks}; return {'email': email, 'about': about, 'remarks': remarks};
} }

View file

@ -76,9 +76,9 @@ abstract class ProfileData {
ProfileData fromMap(Map<String, dynamic> data); ProfileData fromMap(Map<String, dynamic> data);
Map<String, dynamic> toMap(); Map<String, String?> toMap();
Map<String, dynamic> mapWidget(VoidCallback update, BuildContext context); Map<String, Widget?> mapWidget(VoidCallback update, BuildContext context);
ProfileData create(); ProfileData create();
} }

View file

@ -21,8 +21,8 @@ class ItemBuilder {
String key, String key,
String? value, String? value,
Widget? widget, Widget? widget,
Function(String) updateItem, void Function(String) updateItem,
Function(String?) saveItem, void Function(String?) saveItem,
) { ) {
if (widget == null) { if (widget == null) {
var controller = TextEditingController( var controller = TextEditingController(
@ -55,7 +55,7 @@ class ItemBuilder {
Widget buildPassword( Widget buildPassword(
String key, String key,
TextEditingController controller, TextEditingController controller,
Function(String?) onChanged, void Function(String?) onChanged,
String? Function(String?) validator, String? Function(String?) validator,
) { ) {
var inputDecoration = var inputDecoration =

View file

@ -16,49 +16,39 @@ class ItemList {
this.itemBuilder, this.itemBuilder,
this.itemBuilderOptions, this.itemBuilderOptions,
}) { }) {
for (var item in items.entries) { var itemBuilder = this.itemBuilder ?? builder;
widgets.addAll({
item.key: itemBuilder == null widgets = {
? builder.build( for (var item in items.entries) ...{
item.key, item.key: itemBuilder.build(
item.value, item.key,
typeMap[item.key], item.value,
(value) { typeMap[item.key],
saveProfile(); (value) {
}, saveProfile();
(value) { },
updateProfile(item.key, value); (value) {
}, updateProfile(item.key, value);
) },
: 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. /// Gets the map of item keys and their corresponding widgets.
Map<String, Widget> getItemList() => widgets; Map<String, Widget> getItemList() => widgets;
/// Map containing item keys and their values. /// Map containing item keys and their values.
final Map<String, dynamic> items; final Map<String, String?> items;
/// Map containing item keys and their types. /// Map containing item keys and their types.
final Map<String, dynamic> typeMap; final Map<String, Widget?> typeMap;
/// Function to update the profile with a specific item's value. /// 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. /// Function to save the profile after an item value is updated.
final Function() saveProfile; final void Function() saveProfile;
/// Builder for custom item widgets. /// Builder for custom item widgets.
final ItemBuilder? itemBuilder; final ItemBuilder? itemBuilder;
@ -70,7 +60,7 @@ class ItemList {
final GlobalKey<FormState> formKey; final GlobalKey<FormState> formKey;
/// Map containing item keys and their corresponding widgets. /// Map containing item keys and their corresponding widgets.
Map<String, Widget> widgets = {}; late final Map<String, Widget> widgets;
/// `builder` is an instance of `ItemBuilder` which is used /// `builder` is an instance of `ItemBuilder` which is used
/// to build the items in the list. /// to build the items in the list.

View file

@ -13,7 +13,7 @@ class TestProfileData extends ProfileData {
String? email; String? email;
@override @override
Map<String, dynamic> mapWidget( Map<String, Widget?> mapWidget(
VoidCallback update, VoidCallback update,
BuildContext context, BuildContext context,
) => ) =>
@ -27,7 +27,7 @@ class TestProfileData extends ProfileData {
); );
@override @override
Map<String, dynamic> toMap() => { Map<String, String?> toMap() => {
'email': email, 'email': email,
}; };