I am trying to read and write data in appwrite for my expense manager app ,since this is my first time using a database i am unable to get it right. i have written these two functions :-
try {
final document = await databases.createDocument(
databaseId: constants.database_ID,
collectionId: constants.collection_ID,
documentId: ID.unique(),
data: {'Reason': reason, 'Date': DateTime.now(),'Amount': amount,},
permissions: [
Permission.read(Role.any()),
Permission.write(Role.any()),
],
);
} on AppwriteException catch (e) {
print(e);
}
}```
and
``` Future<List<Document>> getData() async {
final Future<DocumentList> result = databases.listDocuments(
databaseId: constants.database_ID,
collectionId: constants.collection_ID,
);
result.then((result) {
return result.data.documents;
}).catchError((error) {
print(error.response);
});
return [];
}```
and from another class i am calling getdata as
//Function to map data from document of Appwrite
``` void fetchData() async {
try {
final List<dynamic> data = await AuthState().getData();
setState(() {
// Convert the fetched data into Expense objects and update the expenses list
expenses = data.map((item) {
return expense(
amount: item['amount'],
date: item['date'],
type: item['type'],
);
}).toList();
});
} catch (e) {
print('Failed to fetch expenses: $e');
}
}```
but i am getting an error stating that the list that iam trying to print is of 0 length
Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting
thankyoufor letting me know i will do the same
should i resend the code blocks with the same
Your getData() returns an empty list. To make things less confusing, I suggest you avoid using them/catchError and use try/await/catch instead
You can edit the post
I this right .
try {
final DocumentList result = await databases.listDocuments(
databaseId: constants.database_ID,
collectionId: constants.collection_ID,
);
return result.documents;
} catch (error) {
print(error);
return [];
}
}}```
but it is still showing the same error
What exactly is the error?
this is the error that i got :- RangeError (index): Invalid value: Valid value range is empty: 0
and the screen crashes
Where is this occurring?
this occurs when i try to call listview.builder
child: ListView.builder(itemBuilder:(context,index){
itemCount: expenses.length;
final expense = expenses[index];
final amount = expense.amount;
final reason = expense.type;
return ListTile(
leading: Text(reason),
title: Text(amount),
);
}),
)```
I have cleared the error but i still not getting the expense and type to be on screen
child: ListView.builder(
itemCount: expenses.length,
itemBuilder: (context, index) {
final expense = expenses[index];
final amount = expense.amount;
final reason = expense.type;
return ListTile(
leading: Text(reason),
title: Text(amount),
);
}),
)```
this is the updated code
Set a breakpoint at the returns here to see which return is called and what is returned by the list documents call
Recommended threads
- Error with realtime channels
I'm performing a subscription to realtime channels, and after a few seconds I get an exception with this error: {\"type\":\"error\",\"data\":{\"code\":1008,\"me...
- Which flutter SDK version for Self Hoste...
Hi all, Is there a good way to figure out which version of flutter SDK and Dart SDK is current for latest available self-hosted 1.8.0 ? I know new features are...
- redirect_uri errors on flutter client
Hi all, I'm using the flutter client for my app to do appwrite auth and use the JWTs to send to my backend. When I try to sign in with SSO, I get this: https:/...