Created
July 31, 2022 06:55
-
-
Save nextdev1111/a689acc7fe3cabb85d059db7de6b8bdc 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/material.dart'; | |
import 'package:flutter_supabase_yt_1/models/models.dart'; | |
import 'package:flutter_supabase_yt_1/screens/create_screen.dart'; | |
import 'package:flutter_supabase_yt_1/screens/home_screen.dart'; | |
import 'package:flutter_supabase_yt_1/screens/screens.dart'; | |
import 'package:flutter_supabase_yt_1/utils/utils.dart'; | |
import 'package:supabase_flutter/supabase_flutter.dart'; | |
class ReadScreen extends StatelessWidget { | |
const ReadScreen({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
List<Todo>? _todos; | |
SupabaseDataManager supabaseDataManager = SupabaseDataManager(); | |
Future<List<Todo>> readData() async { | |
PostgrestResponse<dynamic> res = await supabaseDataManager.readData(); | |
if (res.error != null) { | |
throw Exception(res.error!.message); | |
} | |
// Todo --> [Todo(id: 1, title: 'This'),Todo(id: 2, title: 'fafdaf')] | |
// Map --> [{'id' : 1, 'title': 'This'},{'id' : 2, 'title': 'adfafaf'}] | |
return (res.data as List<dynamic>).map((e) => Todo.fromMap(e)).toList(); | |
} | |
return Scaffold( | |
appBar: AppBar( | |
actions: [ | |
IconButton( | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (route) => const CreateScreen())); | |
}, | |
icon: const Icon(Icons.create)) | |
], | |
), | |
body: FutureBuilder( | |
future: readData(), | |
builder: ((context, snapshot) { | |
if (snapshot.hasError) { | |
return Center(child: Text(snapshot.error.toString())); | |
} else if (snapshot.hasData) { | |
return ListView.builder( | |
itemCount: (snapshot.data! as List<Todo>).length, | |
itemBuilder: ((BuildContext context, int index) { | |
Todo todo = (snapshot.data! as List<Todo>)[index]; | |
return ListTile( | |
leading: Text(todo.id.toString()), | |
title: Text(todo.title), | |
onTap: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (route) => EditScreen(todo: todo))); | |
}, | |
trailing: IconButton( | |
onPressed: () async { | |
var res = await supabaseDataManager.deleteTodo(todo.id!); | |
if (res.error != null) { | |
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
content: Text('Error: ${res.error!.message}'))); | |
} | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar(content: Text('Deleted ${todo.title}'))); | |
}, | |
icon: Icon(Icons.delete), | |
), | |
); | |
}), | |
); | |
} | |
// until the data has been loaded, we will see this progess indicator. | |
return const Center(child: CircularProgressIndicator()); | |
}), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment