needHelp
needHelp

Reputation: 599

Error while fetching data from firestore using flutter

Have tried almost all the solutions from stack overflow I am able to write data in the firestore but after implementing the fetch function I am not able to fetch the data that I uploaded

What I expect to do is: I have uploaded a form user input data in the firestore and want to fetch those data from firestore

If you want you can find the link to entire code here

Here's the error:

Closure call with mismatched arguments: function 'data'
Receiver: Closure: () => Map<String, dynamic> from Function 'data':.
Tried calling: data
Found: data() => Map<String, dynamic>

Here's a quick snapshot of all methods that I have written so far

String name, age, phone;
  crudMethods crudObjs = crudMethods();
  QuerySnapshot rideNow;
  List userProfilesList = [];

  @override
  void initState() {
    
    fetchDatabaseList();
    super.initState();
  }

  fetchDatabaseList() async {
    dynamic resultant = await crudMethods().getData();

    if (resultant == null) {
      print('Unable to retrieve');
    } else {
      setState(() {
        userProfilesList = resultant;
      });
    }
  }

body: Column(children[
 Expanded(
                child: ListView.builder(
                  itemCount: userProfilesList.length,
                  itemBuilder: (context, i) {
                    return ListTile(
                      title: Text(userProfilesList[i].data['name']),
                      subtitle: Text(userProfilesList[i].data['age']),
                    );
                  },
                ),
              ),
            
]);

Here's the fetch data method:

Future getData() async {
    List itemsList = [];
    final CollectionReference profileList =
        FirebaseFirestore.instance.collection('Ride Now Details');
    try {
      await profileList.get().then((querySnapshot) {
        querySnapshot.docs.forEach((element) {
          itemsList.add(element.data);
        });
      });
      return itemsList;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

If you have a better way around by which I can fetch the data can please help me out with this and please help me solve this error.

Upvotes: 1

Views: 383

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317412

data is a method, not a property. So you have to call it like a method.

itemsList.add(element.data());

As you can see from the API documentation, it returns an Map<String, dynamic> with the fields and values in the DocumentSnapshot.

Upvotes: 3

Related Questions