Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

added listView instead of column and await instead of then

Aniket Singhb3649fd17274
diff --git a/examples/todo-list/flutter-todo-list/lib/main.dart b/examples/todo-list/flutter-todo-list/lib/main.dart
index ede922e9a5e..7ffdff4f02a 100644
--- a/examples/todo-list/flutter-todo-list/lib/main.dart
+++ b/examples/todo-list/flutter-todo-list/lib/main.dart
@@ -2,15 +2,14 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:todosupabase/constant.dart';
-import 'package:todosupabase/functions/auth.dart';
import 'package:todosupabase/screens/signup.dart';
import 'package:todosupabase/screens/signin.dart';
import 'package:todosupabase/screens/todo.dart';
void main() async {
await Supabase.initialize(
- url: 'YOUR_SUPABASE_URL',
- anonKey: 'YOUR_SUPABASE_KEY',
+ url: 'YOUR_SUPABASE_URL',
+ anonKey: 'YOUR_SUPABASE_KEY',
);
runApp(const MyApp());
}
diff --git a/examples/todo-list/flutter-todo-list/lib/screens/todo.dart b/examples/todo-list/flutter-todo-list/lib/screens/todo.dart
index 1da6813cde1..6aefbdcecfc 100644
--- a/examples/todo-list/flutter-todo-list/lib/screens/todo.dart
+++ b/examples/todo-list/flutter-todo-list/lib/screens/todo.dart
@@ -14,55 +14,62 @@ class _TodoPageState extends State<TodoPage> {
String userName = "";
bool loading = true;
List<dynamic> todos = [];
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- CrudSupabase.getUser(
- email: AuthSupabase.client.auth.currentUser!.email.toString())
- .then((value) {
- setState(() {
- userName = value[0]['name'];
- });
+ final TextEditingController _taskEditingController = TextEditingController();
+ final TextEditingController _dueEditingController = TextEditingController();
+
+ void initFunc() async {
+ var nameVal = await CrudSupabase.getUser(
+ email: AuthSupabase.client.auth.currentUser!.email.toString());
+ setState(() {
+ userName = nameVal[0]['name'];
});
- CrudSupabase.getTodo().then((value) {
- value.forEach((element) {
- todos.add(element);
- });
+ List<dynamic> value = await CrudSupabase.getTodo();
+ value.forEach((element) {
+ todos.add(element);
});
setState(() {
loading = false;
});
}
+ @override
+ void initState() {
+ // TODO: implement initState
+ super.initState();
+ initFunc();
+ }
+
+ @override
+ void dispose() {
+ super.dispose();
+ _taskEditingController.dispose();
+ _dueEditingController.dispose();
+ }
+
@override
Widget build(BuildContext context) {
- final TextEditingController _taskEditingController =
- TextEditingController();
- final TextEditingController _dueEditingController = TextEditingController();
return Scaffold(
floatingActionButton: FloatingActionButton(
backgroundColor: const Color(0xff33b27b),
onPressed: () {
- displayTextInputDialog(context, () {
- CrudSupabase.addTodo(
+ displayTextInputDialog(context, () async {
+ await CrudSupabase.addTodo(
task: _taskEditingController.text,
due: _dueEditingController.text,
- ).then((value) {
- Navigator.pop(context);
- setState(() {
- loading = true;
- });
- CrudSupabase.getTodo().then((value) {
- todos.clear();
- setState(() {});
- value.forEach((element) {
- todos.add(element);
- });
- });
- setState(() {
- loading = false;
- });
+ );
+ Navigator.pop(context);
+ setState(() {
+ loading = true;
+ });
+
+ List<dynamic> value = await CrudSupabase.getTodo();
+ todos.clear();
+ setState(() {});
+ value.forEach((element) {
+ todos.add(element);
+ });
+ setState(() {
+ loading = false;
});
}, _taskEditingController, _dueEditingController);
},
@@ -90,122 +97,110 @@ class _TodoPageState extends State<TodoPage> {
? const Center(
child: Text("No Task, add new task"),
)
- : Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: todos
- .map(
- (e) => ListTile(
- //Long Press to edit
- onLongPress: () {
- displayTextInputDialog(context, () {
- CrudSupabase.editTask(
- id: e["id"].toString(),
- due: _dueEditingController.text,
- task: _taskEditingController.text)
- .then((value) {
- Navigator.pop(context);
- setState(() {
- loading = true;
- });
- CrudSupabase.getTodo().then((value) {
- todos.clear();
- setState(() {});
- value.forEach((element) {
- todos.add(element);
- });
- });
- setState(() {
- loading = false;
- });
- });
- }, _taskEditingController,
- _dueEditingController);
- },
- title: Text(
- e["task"],
- style: TextStyle(color: Colors.white),
- ),
- subtitle: Text(
- e["due"],
- style: TextStyle(color: Colors.white),
- ),
- leading: GestureDetector(
- onTap: () {
- setState(() {
- loading = true;
- });
- CrudSupabase.deleteTodo(
- id: e["id"].toString())
- .then((value) {
- print("delteed");
- CrudSupabase.getTodo().then((value) {
- todos.clear();
- setState(() {});
- value.forEach((element) {
- todos.add(element);
- });
- });
- setState(() {
- loading = false;
- });
- });
- },
- child: const Icon(
- Icons.delete,
- color: Colors.white,
- ),
- ),
- trailing: GestureDetector(
- onTap: () {
- setState(() {
- loading = true;
- });
- CrudSupabase.taskDone(
- id: e["id"].toString(),
- value: !e["done"])
- .then((value) {
- CrudSupabase.getTodo().then((value) {
- todos.clear();
- setState(() {});
- value.forEach((element) {
- todos.add(element);
- });
- setState(() {
- loading = false;
- });
- });
- });
- },
- child: Container(
- height: 25,
- width: 25,
- decoration: BoxDecoration(
- border: Border.all(
- color: const Color(0xff33b27b),
- width: 2,
- ),
- color: e["done"]
- ? const Color(0xff33b27b)
- : Colors.transparent,
- borderRadius: BorderRadius.circular(5),
- ),
- child: e["done"]
- ? const Center(
- child: Icon(
- Icons.done,
- size: 15,
- color: Colors.white,
- ),
- )
- : const SizedBox(),
+ : ListView.builder(
+ itemCount: todos.length,
+ itemBuilder: (context, index) {
+ return ListTile(
+ //Long Press to edit
+ onLongPress: () {
+ displayTextInputDialog(context, () async {
+ await CrudSupabase.editTask(
+ id: todos[index]["id"].toString(),
+ due: _dueEditingController.text,
+ task: _taskEditingController.text);
+ Navigator.pop(context);
+ setState(() {
+ loading = true;
+ });
+ List<dynamic> value =
+ await CrudSupabase.getTodo();
+ todos.clear();
+ setState(() {});
+ value.forEach((element) {
+ todos.add(element);
+ });
+ setState(() {
+ loading = false;
+ });
+ }, _taskEditingController, _dueEditingController);
+ },
+ title: Text(
+ todos[index]["task"],
+ style: TextStyle(color: Colors.white),
+ ),
+ subtitle: Text(
+ todos[index]["due"],
+ style: TextStyle(color: Colors.white),
+ ),
+ leading: GestureDetector(
+ onTap: () async {
+ setState(() {
+ loading = true;
+ });
+ await CrudSupabase.deleteTodo(
+ id: todos[index]["id"].toString());
+ List<dynamic> value =
+ await CrudSupabase.getTodo();
+ todos.clear();
+ setState(() {});
+ value.forEach((element) {
+ todos.add(element);
+ });
+ setState(() {
+ loading = false;
+ });
+ },
+ child: const Icon(
+ Icons.delete,
+ color: Colors.white,
+ ),
+ ),
+ trailing: GestureDetector(
+ onTap: () async {
+ setState(() {
+ loading = true;
+ });
+ await CrudSupabase.taskDone(
+ id: todos[index]["id"].toString(),
+ value: !todos[index]["done"]);
+ List<dynamic> value =
+ await CrudSupabase.getTodo();
+ todos.clear();
+ setState(() {});
+ value.forEach((element) {
+ todos.add(element);
+ });
+ setState(() {
+ loading = false;
+ });
+ },
+ child: Container(
+ height: 25,
+ width: 25,
+ decoration: BoxDecoration(
+ border: Border.all(
+ color: const Color(0xff33b27b),
+ width: 2,
),
+ color: todos[index]["done"]
+ ? const Color(0xff33b27b)
+ : Colors.transparent,
+ borderRadius: BorderRadius.circular(5),
),
+ child: todos[index]["done"]
+ ? const Center(
+ child: Icon(
+ Icons.done,
+ size: 15,
+ color: Colors.white,
+ ),
+ )
+ : const SizedBox(),
),
- )
- .toList(),
- ),
- ),
+ ),
+ );
+ },
+ )),
);
}
}
diff --git a/examples/todo-list/flutter-todo-list/pubspec.lock b/examples/todo-list/flutter-todo-list/pubspec.lock
index af291b99057..a899da6fff7 100644
--- a/examples/todo-list/flutter-todo-list/pubspec.lock
+++ b/examples/todo-list/flutter-todo-list/pubspec.lock
@@ -50,13 +50,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
- cupertino_icons:
- dependency: "direct main"
- description:
- name: cupertino_icons
- url: "https://pub.dartlang.org"
- source: hosted
- version: "1.0.4"
fake_async:
dependency: transitive
description:
diff --git a/examples/todo-list/flutter-todo-list/pubspec.yaml b/examples/todo-list/flutter-todo-list/pubspec.yaml
index bb6f1b9d1c4..4fbe0d09274 100644
--- a/examples/todo-list/flutter-todo-list/pubspec.yaml
+++ b/examples/todo-list/flutter-todo-list/pubspec.yaml
@@ -32,7 +32,6 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
- cupertino_icons: ^1.0.2
supabase_flutter: ^0.3.1+1
flutter_svg: ^1.0.3