Firestore DocumentReference mapping

638 views
Skip to first unread message

Can Aktepe

unread,
Jul 17, 2020, 4:30:43 PM7/17/20
to Flutter Development (flutter-dev)
Hi all,

I using firestore with '@flutter' first time. There are 2 collections and I want to use  DocumentReference type. Collections does looking as below. I added a DocumentReference type field to Activities collection. this reference do store a user fromuser collection. Can you help me about mapping  2 collections a single model.

activities.jpg

activitymodel.jpg

users.jpg

usermodel.jpg





Suzuki Tomohiro

unread,
Jul 17, 2020, 11:00:09 PM7/17/20
to Can Aktepe, Flutter Development (flutter-dev)
Would you explain the problem you observe?
(I’m seeing that you’re passing a document reference to a function that takes a map. But let’s start with the problem you observe)


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/b822a72e-cb29-460c-b600-ec6598d1e25eo%40googlegroups.com.

Can Aktepe

unread,
Jul 18, 2020, 2:40:43 AM7/18/20
to Flutter Development (flutter-dev)
Hi,

actually, when I call activities data, I want to include documentreference data in the response.

Suzuki Tomohiro

unread,
Jul 18, 2020, 7:22:43 AM7/18/20
to Can Aktepe, Flutter Development (flutter-dev)
Then iclude DocumentReference data in the response. Would you explain the problem you observe?


On Sat, Jul 18, 2020 at 02:40 Can Aktepe <akte...@gmail.com> wrote:
Hi,

actually, when I call activities data, I want to include documentreference data in the response.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Can Aktepe

unread,
Jul 18, 2020, 7:29:36 AM7/18/20
to Flutter Development (flutter-dev)
I don't know how can I include it in to response

Suzuki Tomohiro

unread,
Jul 18, 2020, 7:38:54 AM7/18/20
to Can Aktepe, Flutter Development (flutter-dev)
data['author'] is the document reference (assuming you store a document reference there)


On Sat, Jul 18, 2020 at 07:29 Can Aktepe <akte...@gmail.com> wrote:
I don't know how can I include it in to response

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

akte...@gmail.com

unread,
Jul 18, 2020, 10:43:48 AM7/18/20
to Flutter Development (flutter-dev)
actually, I asked a basic question, I want to cast DocumentReference object in to Activity model, 
as a result;

I want to show like result to below.

ACTIVITY MODEL
    active : true,
    author : {
         id : 1,
         email : "em...@email.com"
    },
    complex : "complex text",
    maxPlayer : 14,
    players : [
         0 : "Player 1",
         1 : "Player 2"
    ],
    title : "Title text"
}

18 Temmuz 2020 Cumartesi tarihinde saat 14:38:54 UTC+3 itibarıyla suz...@gmail.com şunları yazdı:

Suzuki Tomohiro

unread,
Jul 18, 2020, 10:52:30 AM7/18/20
to akte...@gmail.com, Flutter Development (flutter-dev)
Still not sure what problem you are facing. Are you looking for DocumentReference.get() ?

final authorDocumentReference = data[‘author’];
final author = authorDocumentReference.get();
final user = User.fromMap(author)


akte...@gmail.com

unread,
Jul 18, 2020, 11:21:10 AM7/18/20
to Flutter Development (flutter-dev)
yes, this is working. Thank you. However, How can I do async call in the Factory function?
author variable does return as Future<DocumentSnapshot>

 factory Activity.fromMap(Map data) {
    // var x = User.fromMap(data['author']);

    var activity = Activity(
      title: data['title'],
      complex: data['complex'],
      maxPlayer: data['maxPlayer'],
      active: data['active'],
      players: List.from(
        data['players'],
      ),
    );

    final authorDocumentReference = data["author"] as DocumentReference;
    final author = authorDocumentReference.get();
    author.then((value) => {
          activity.author = User.fromMap(value.data),
          print(activity.author.email)
        });


    return activity;
  }
18 Temmuz 2020 Cumartesi tarihinde saat 17:52:30 UTC+3 itibarıyla suz...@gmail.com şunları yazdı:

Suzuki Tomohiro

unread,
Jul 18, 2020, 11:45:28 AM7/18/20
to akte...@gmail.com, Flutter Development (flutter-dev)
Good. Now the problem is clear: you want to use async in a factory method but it doesn’t allow async.

Use async static method.

Reply all
Reply to author
Forward
0 new messages