Created
July 19, 2022 18:35
-
-
Save felipecastrosales/89ec0c7f4b520ab76759965a4fdf54cb to your computer and use it in GitHub Desktop.
Cart Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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