
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
- Sites 30MB limit from GitHub
I’m deploying a site from github as Other type on the Hobby plan. It is actually a Flutter web app but it’s in a subdirectory with the root being an html landin...
- Google OAuth2 Login Gets Stuck in Redire...
I'm facing an issue with the Google OAuth2 login flow on my Flutter Android app using the Appwrite SDK. After a successful sign-in with Google, the browser ente...
- Facebook OAuth with Appwrite Cloud fails...
I’m integrating Facebook login in my Flutter app using Appwrite Cloud. Google OAuth works fine, but I’m stuck with Facebook. Here’s what happens: When I log i...
