Skip to content

Instantly share code, notes, and snippets.

@nextdev1111
Created July 31, 2022 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nextdev1111/f0b10ac66205e8838621ba53950bdf54 to your computer and use it in GitHub Desktop.
Save nextdev1111/f0b10ac66205e8838621ba53950bdf54 to your computer and use it in GitHub Desktop.
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