Created
July 31, 2022 06:54
-
-
Save nextdev1111/f0b10ac66205e8838621ba53950bdf54 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 'package:flutter_supabase_yt_1/models/models.dart'; // this is being used to import the todo.dart from models file. | |
import 'package:supabase_flutter/supabase_flutter.dart'; | |
class SupabaseDataManager { | |
// create function which takes one argument of Todo | |
Future<PostgrestResponse<dynamic>> createData(Todo todo) async { | |
PostgrestResponse<dynamic> res = await Supabase.instance.client | |
.from('todos') | |
// here 👇 you need to make todo.toMap() because we need to make Todo model to map --> eg Todo(title: 'This is first todo') -> {'title': 'This is first todo'} | |
.insert(todo.toMap()) | |
.execute(); | |
return res; | |
} | |
// read function | |
Future<PostgrestResponse<dynamic>> readData() async { | |
PostgrestResponse<dynamic> res = await Supabase.instance.client | |
.from('todos') | |
.select('id, title') | |
.execute(); | |
return res; | |
} | |
// update function | |
Future<PostgrestResponse<dynamic>> updateTodo(Todo todo) async { | |
// upsert or update | |
PostgrestResponse<dynamic> res = await Supabase.instance.client | |
.from('todos') | |
.update({'title': todo.title}) | |
.eq('id', todo.id) | |
.execute(); | |
return res; | |
} | |
// delete function | |
Future<PostgrestResponse<dynamic>> deleteTodo(int todoId) async { | |
PostgrestResponse<dynamic> res = await Supabase.instance.client | |
.from('todos') | |
.delete() | |
.eq('id', todoId) | |
.execute(); | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment