简体   繁体   中英

How do you get a documentSnapshot from firestore over a cloud function in flutter?

a bit of intro first, I need to get the documentSnapshot and not only the data of a record which is in Firestore because I have pagination in my app, and to do that, when you query you need to send the last documentSnapshot from where your query will send the 10 next records.

So, this worked fine with the SDK, but I want to implement Cloud Functions now, and when I try to respond with a documentSnapshot from the cloud function I am not able to get the documentSnapshot in Flutter.

This is the portion of code where the issue is:

final function = _cloudFunctions.getHttpsCallable(
  functionName: 'createRecord',
);
try {
  final response = await function.call(data);
  final DocumentSnapshot doc = response.data;
  return doc;
} catch (e) {
  print(e)
  return null;
}

Whith this code I get the error: type '_InternalLinkedHashMap' is not a subtype of type 'DocumentSnapshot'

Is there a workaround or I am doing something wrong?

The response from a callable Cloud Function is not going to be a strongly typed DocumentSnapshot . That's what the error message is trying to tell you. You're getting a map type object, and you have to deal with that object as such. You can't just force it to be a DocumentSnapshot.

If you're using your callable function as a tool for pagination, you won't be able to pass DocumentSnapshot objects back and forth between the client and function. You're going to have to pass the function enough information to paginate without providing a DocumentSnapshot.

A DocumentSnapshot is not actually required to paginate - what you do need is enough information to tell the query to pick up where it left off, as shown in the documentation . This involves using startAt or startAfter using information from the last document seen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM