Created
July 19, 2022 18:36
-
-
Save felipecastrosales/69903db2da789dedf957a589a543c850 to your computer and use it in GitHub Desktop.
Itens 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:hive_flutter/hive_flutter.dart'; | |
part 'itens.g.dart'; | |
@HiveType(typeId: 0) | |
class Itens extends HiveObject { | |
@HiveField(0) | |
final String id; | |
@HiveField(1) | |
final String nome; | |
@HiveField(2) | |
final String imagem; | |
@HiveField(3) | |
final double valor; | |
@HiveField(4) | |
int? quantidade; | |
Itens({ | |
required this.id, | |
required this.nome, | |
required this.imagem, | |
required this.valor, | |
this.quantidade = 0, | |
}); | |
Itens copyWith({ | |
String? id, | |
String? nome, | |
String? imagem, | |
double? valor, | |
int? quantidade, | |
}) { | |
return Itens( | |
id: id ?? this.id, | |
nome: nome ?? this.nome, | |
imagem: imagem ?? this.imagem, | |
valor: valor ?? this.valor, | |
quantidade: quantidade ?? this.quantidade, | |
); | |
} | |
Map<String, dynamic> toMap() { | |
return <String, dynamic>{ | |
'id': id, | |
'nome': nome, | |
'imagem': imagem, | |
'valor': valor, | |
}; | |
} | |
factory Itens.fromMap(Map<String, dynamic> map) { | |
return Itens( | |
id: map['id'] as String, | |
nome: map['nome'] as String, | |
imagem: map['imagem'] as String, | |
valor: double.parse(map['valor'].toString()), | |
); | |
} | |
String toJson() => json.encode(toMap()); | |
factory Itens.fromJson(String source) => | |
Itens.fromMap(json.decode(source) as Map<String, dynamic>); | |
@override | |
String toString() { | |
return 'Itens(id: $id, nome: $nome, imagem: $imagem, valor: $valor)'; | |
} | |
@override | |
bool operator ==(Object other) { | |
if (identical(this, other)) return true; | |
return other is Itens && | |
other.id == id && | |
other.nome == nome && | |
other.imagem == imagem && | |
other.valor == valor; | |
} | |
@override | |
int get hashCode { | |
return id.hashCode ^ nome.hashCode ^ imagem.hashCode ^ valor.hashCode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment