Created
July 24, 2022 04:08
-
-
Save nextdev1111/9adb497006e9363091adc9611cfcf944 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/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( | |
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( | |
title: Text(todo.title), | |
); | |
}), | |
); | |
} | |
// 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