formattazione testo da e verso json
This commit is contained in:
22
lib/core/utils/string_extensions.dart
Normal file
22
lib/core/utils/string_extensions.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
extension MyStringExtensions on String? {
|
||||
// Gestiamo anche il nullable per sicurezza
|
||||
String myFormat() {
|
||||
if (this == null || this!.trim().isEmpty) return '';
|
||||
|
||||
return this!
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.where((word) => word.isNotEmpty) // Evita problemi con doppi spazi
|
||||
.map((word) {
|
||||
if (word.contains('iphone') || word.contains('ipad')) {
|
||||
return word.replaceFirst('ip', 'iP');
|
||||
}
|
||||
if (word.contains('imac')) {
|
||||
return word.replaceFirst('im', 'iM');
|
||||
}
|
||||
return word[0].toUpperCase() + word.substring(1);
|
||||
})
|
||||
.join(' ');
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/utils/string_extensions.dart';
|
||||
|
||||
class CustomerModel extends Equatable {
|
||||
final String? id; // Bigint in SQL
|
||||
@@ -81,7 +82,7 @@ class CustomerModel extends Equatable {
|
||||
createdAt: json['created_at'] != null
|
||||
? DateTime.parse(json['created_at'])
|
||||
: null,
|
||||
nome: json['nome'],
|
||||
nome: (json['nome'] as String).myFormat(),
|
||||
telefono: json['telefono'],
|
||||
email: json['email'],
|
||||
note: json['note'] ?? '',
|
||||
@@ -98,9 +99,9 @@ class CustomerModel extends Equatable {
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'nome': nome,
|
||||
'nome': nome.toLowerCase(),
|
||||
'telefono': telefono,
|
||||
'email': email,
|
||||
'email': email.toLowerCase(),
|
||||
'note': note,
|
||||
if (dataUltimoContatto != null)
|
||||
'data_ultimo_contatto': dataUltimoContatto!.toIso8601String(),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/utils/string_extensions.dart';
|
||||
|
||||
class BrandModel extends Equatable {
|
||||
final String? id;
|
||||
@@ -18,7 +19,7 @@ class BrandModel extends Equatable {
|
||||
factory BrandModel.fromJson(Map<String, dynamic> json) {
|
||||
return BrandModel(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
name: (json['name'] as String).myFormat(),
|
||||
companyId: json['company_id'] as String,
|
||||
isActive: json['is_active'] as bool? ?? true,
|
||||
createdAt: json['created_at'] != null
|
||||
@@ -30,7 +31,7 @@ class BrandModel extends Equatable {
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'name': name,
|
||||
'name': name.toLowerCase(),
|
||||
'company_id': companyId,
|
||||
'is_active': isActive,
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/utils/string_extensions.dart';
|
||||
|
||||
class ModelModel extends Equatable {
|
||||
final String? id;
|
||||
@@ -20,8 +21,8 @@ class ModelModel extends Equatable {
|
||||
factory ModelModel.fromJson(Map<String, dynamic> json) {
|
||||
return ModelModel(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
nameWithBrand: json['name_with_brand'] as String,
|
||||
name: (json['name'] as String).myFormat(),
|
||||
nameWithBrand: (json['name_with_brand'] as String).myFormat(),
|
||||
brandId: json['brand_id'] as String,
|
||||
isActive: json['is_active'] as bool? ?? true,
|
||||
createdAt: json['created_at'] != null
|
||||
@@ -33,8 +34,8 @@ class ModelModel extends Equatable {
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'name': name,
|
||||
'name_with_brand': nameWithBrand,
|
||||
'name': name.toLowerCase(),
|
||||
'name_with_brand': nameWithBrand.toLowerCase(),
|
||||
'brand_id': brandId,
|
||||
'is_active': isActive,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user