FlutterFirebase
FlutterFirebase

Reputation: 2353

Chat messages not ordering correctly

I am use Firestore and Flutter for chat app. It work ok but I see this issue. Sometimes messages not display in order. For example usually they are sorted so most recent at bottom. But I test on iOS and Android simulator and see sometimes messages not display in order. For example I send messages on iOS and everything is ok (in order). Then I send on different simulator (for example Android) and the messages show at top and then start descending (on top of messages sent on iOS).

Here my code:

            child: new FirestoreAnimatedList(
              query: reference
                  .orderBy('timestamp', descending: true)
                  .snapshots(),
              padding: new EdgeInsets.all(8.0),
              reverse: true,
              itemBuilder: (_, DocumentSnapshot snapshot,
                  Animation<double> animation, int x) {
                return new Chat(
                    snapshot: snapshot, animation: animation);
              },
            ),

'timestamp': DateTime.now(),

I have try this but same issue:

'timestamp': DateTime.now().millisecondsSinceEpoch.toString()

I look for weeks for answer but no find. Anyone can help?

Upvotes: 2

Views: 2467

Answers (1)

Jake Lee
Jake Lee

Reputation: 7989

You're likely encountering this issue because the devices are reporting different times.

To solve this, use the server time instead of local time. This is done by setting your timestamp field to FieldValue.serverTimestamp().

Documentation.

Upvotes: 2

Related Questions