Back

getting data

  • 0
  • Flutter
Jhalak_Upadhyay
3 Jun, 2023, 05:46

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 :-

TypeScript
    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
TL;DR
The user is trying to display expenses in a ListView using ListView.builder, but is getting a RangeError. They suspect the issue is with the list returned by the getData() function. The solution provided is to avoid using catchError and use try/await/catch instead. Additionally, the user is advised to use 3 back ticks for multi-line code blocks.
Drake
3 Jun, 2023, 05:54

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

Jhalak_Upadhyay
3 Jun, 2023, 05:55

thankyoufor letting me know i will do the same

Jhalak_Upadhyay
3 Jun, 2023, 05:56

should i resend the code blocks with the same

Drake
3 Jun, 2023, 05:57

Your getData() returns an empty list. To make things less confusing, I suggest you avoid using them/catchError and use try/await/catch instead

Drake
3 Jun, 2023, 05:57

You can edit the post

Jhalak_Upadhyay
3 Jun, 2023, 06:05

I this right .

Jhalak_Upadhyay
3 Jun, 2023, 06:05
TypeScript
    try {
      final DocumentList result = await databases.listDocuments(
        databaseId: constants.database_ID,
        collectionId: constants.collection_ID,
      );
      return result.documents;
    } catch (error) {
      print(error);
      return [];
    }
  }}```
Jhalak_Upadhyay
3 Jun, 2023, 06:07

but it is still showing the same error

Drake
3 Jun, 2023, 06:11

What exactly is the error?

Jhalak_Upadhyay
3 Jun, 2023, 06:11

this is the error that i got :- RangeError (index): Invalid value: Valid value range is empty: 0

Jhalak_Upadhyay
3 Jun, 2023, 06:11

and the screen crashes

Drake
3 Jun, 2023, 06:12

Where is this occurring?

Jhalak_Upadhyay
3 Jun, 2023, 06:15

this occurs when i try to call listview.builder

Jhalak_Upadhyay
3 Jun, 2023, 06:15
TypeScript
            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),
              );
            }),
          )```
Jhalak_Upadhyay
3 Jun, 2023, 07:07

I have cleared the error but i still not getting the expense and type to be on screen

Jhalak_Upadhyay
3 Jun, 2023, 07:07
TypeScript
            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),
                  );
                }),
          )```
Jhalak_Upadhyay
3 Jun, 2023, 07:08

this is the updated code

Drake
3 Jun, 2023, 14:35

Set a breakpoint at the returns here to see which return is called and what is returned by the list documents call

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