Last active
July 24, 2022 04:03
-
-
Save nextdev1111/d437e5985cbd8c52dda7c331f84cc1eb to your computer and use it in GitHub Desktop.
This file contains 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'; | |
class Todo { | |
final int? | |
id; // we are keeping the id to be nullable because we will use Todo type object as argument in create function. | |
// So there, we will not pass any id but the supabase will automatically generate it for us. | |
final String title; | |
Todo({ | |
this.id, | |
required this.title, | |
}); | |
Todo copyWith({ | |
int? id, | |
String? title, | |
}) { | |
return Todo( | |
id: id ?? this.id, | |
title: title ?? this.title, | |
); | |
} | |
Map<String, dynamic> toMap() { | |
final result = <String, dynamic>{}; | |
if (id != null) { | |
result.addAll({'id': id}); | |
} | |
result.addAll({'title': title}); | |
return result; | |
} | |
factory Todo.fromMap(Map<String, dynamic> map) { | |
return Todo( | |
id: map['id']?.toInt(), | |
title: map['title'] ?? '', | |
); | |
} | |
String toJson() => json.encode(toMap()); | |
factory Todo.fromJson(String source) => Todo.fromMap(json.decode(source)); | |
@override | |
String toString() => 'Todo(id: $id, title: $title)'; | |
@override | |
bool operator ==(Object other) { | |
if (identical(this, other)) return true; | |
return other is Todo && other.id == id && other.title == title; | |
} | |
@override | |
int get hashCode => id.hashCode ^ title.hashCode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment