Skip to content
Back

How do I fetch data from the database? I am new to Appwrite

  • 1
  • Databases
  • Flutter
Darkhorse
8 Jun, 2023, 19:43

Here is the code I have tried

class AppwriteServices { Databases databases = Databases(client);

Future readBooks() async { final results = databases.listDocuments( databaseId: '647b2d9c3517092fd026', collectionId: '647b2dac72f880f4386d');

TypeScript
results.then((value) {
  print(value.toMap());
}).catchError((error) {
  print(error);
});

} }

##############

class HomeScreen extends StatelessWidget { const HomeScreen({super.key});

@override Widget build(BuildContext context) { return Scaffold( body: Container( child: FutureBuilder( future: AppwriteServices().readBooks(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { List records = snapshot.data['documents'];
return ListView.builder( itemCount: records.length, itemBuilder: (context, int index) { return Card( child: ListTile( title: Text(records[index]['firstname']), ), ); }, ); } else { return Center(child: CircularProgressIndicator()); } }, ), )); } }

TL;DR
The user is experiencing an issue where their function is not returning any data, causing an error in their widget tree. They shared their code and mentioned that it's best to format code using backticks. To fetch data from the database, the user needs to make sure their function `readBooks()` returns a value. Currently, it only prints the data but doesn't return it. To fix this, they can modify their `readBooks()` function to return the value retrieved from the database. Here is the updated code: ```dart Future<List> readBooks() async { final results = await databases.listDocuments( database
Drake
9 Jun, 2023, 00:57

your function never returned any data so it can't be used in your widget tree.

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more