Skip to content

Instantly share code, notes, and snippets.

@felipecastrosales
Created July 19, 2022 18:35
Show Gist options
  • Save felipecastrosales/89ec0c7f4b520ab76759965a4fdf54cb to your computer and use it in GitHub Desktop.
Save felipecastrosales/89ec0c7f4b520ab76759965a4fdf54cb to your computer and use it in GitHub Desktop.
Cart Model
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:app/data/models/menu/itens.dart';
class Cart {
String? referencia;
List<Itens>? vendaMobileItens;
double? total;
int? quantidade = 0;
Cart({
required this.referencia,
required this.vendaMobileItens,
required this.total,
this.quantidade,
});
factory Cart.fromMap(Map<String, dynamic> data) => Cart(
referencia: data['referencia'] ?? '**',
vendaMobileItens: (data['vendaMobileItens'] ?? <Itens>[])
?.map((items) => Itens.fromMap(items as Map<String, dynamic>))
.toList(),
total: data['total'] as double?,
);
Map<String, dynamic> toMap() => {
'referencia': referencia,
'vendaMobileItens':
vendaMobileItens!.map((items) => items.toMap()).toList(),
'total': total,
};
factory Cart.fromJson(Map<String, dynamic> json) {
return Cart.fromMap(json);
}
String toJson() => json.encode(toMap());
@override
String toString() {
return 'Cart(referencia: $referencia, vendaMobileItens: $vendaMobileItens, total: $total)';
}
Cart copyWith({
String? referencia,
List<Itens>? vendaMobileItens,
double? total,
int? quantidade,
}) {
return Cart(
referencia: referencia ?? this.referencia,
vendaMobileItens: vendaMobileItens ?? this.vendaMobileItens,
total: total ?? this.total,
quantidade: quantidade ?? this.quantidade,
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Cart &&
other.referencia == referencia &&
listEquals(other.vendaMobileItens, vendaMobileItens) &&
other.total == total &&
other.quantidade == quantidade;
}
@override
int get hashCode {
return referencia.hashCode ^
vendaMobileItens.hashCode ^
total.hashCode ^
quantidade.hashCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment