vedartm / paginate_firestore

A flutter package to simplify pagination with firestore data 🗃
https://pub.dev/packages/paginate_firestore
MIT License
113 stars 136 forks source link

How to cast Document snapshot to my Model. #37

Closed mohdtaha60 closed 3 years ago

mohdtaha60 commented 3 years ago

Thank you for this amazing library. Just wanna know what is the proper method to cast documentSnapshot to my Model i.e Product. tried this inside item itemBuilder, but doesn't work.
Product products =documentSnapshot.data() as Product;

Error is _InternalLinkedHashMap<String, dynamic>' is not a subtype of type documentSnapshot.data. Update: Ok, seems am doing it wrong. Can anyone help me mapping the snapshot data to an object

vedartm commented 3 years ago

Hey @mohdtaha60. You will have to manually cast the data from the documentSnapshot (which is of Map type). Check this below example to get an idea.

        itemBuilder: (index, context, documentSnapshot) {
          final data = documentSnapshot.data();
          final product = Product(
            id: data['id'],
            name: data['name'],
            quantity: data['quantity'],
          );
          return ListTile(
            leading: CircleAvatar(child: Icon(Icons.person)),
            title: Text(product.name),
            subtitle: Text(product.quantity),
          );
        },
vedartm commented 3 years ago

Feel free to reopen the issue if you face any related issue. Thanks for using the package 🎉

mohdtaha60 commented 3 years ago

Hey @mohdtaha60. You will have to manually cast the data from the documentSnapshot (which is of Map type). Check this below example to get an idea.

        itemBuilder: (index, context, documentSnapshot) {
          final data = documentSnapshot.data();
          final product = Product(
            id: data['id'],
            name: data['name'],
            quantity: data['quantity'],
          );
          return ListTile(
            leading: CircleAvatar(child: Icon(Icons.person)),
            title: Text(product.name),
            subtitle: Text(product.quantity),
          );
        },

Thanks alottt dude, it helped me alot.