
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
- Flutter OAuth2 Google does not return to...
When the flow starts, the browser opens, I select an account, and it keeps showing: """ Page not found The page you're looking for doesn't exist. `general_rout...
- Redirect URL sends HTTP instead of HTTPS...
I am not sure since when this issue is present, but my Google and Apple redirect URI are no longer pointing to the HTTPS redirect URI when I try to use OAuth. ...
- Failing to run document operations on sd...
Could someone point me in the right direction I'm going in cirlces. I have a problem with sdks and my self-hosted server in production (for ~3 years) I have bee...
