Home > Enterprise >  firebase firestore chat timestamp order not working corectly
firebase firestore chat timestamp order not working corectly

Time:10-24

below is my code, i have a chat made in flutter and it should be sorted by timestamp however it is still seemingly random, on firebase it is recording the correct time stamp in json and i thought i had it coded correctly with the firestore.collection order by call. image attached shows the messages they should be ordered 1-6enter image description here

import 'package:flutter/material.dart';
import 'package:bardsf/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
late String messageText;


class BodyBuildingChat extends StatefulWidget {
  static const String id = 'body_building_chat';
  @override
  _BodyBuildingChatState createState() => _BodyBuildingChatState();
}

class _BodyBuildingChatState extends State<BodyBuildingChat> {
  final messageTextController = TextEditingController();
  final _firestore = FirebaseFirestore.instance;
  final _auth = FirebaseAuth.instance;
  late User loggedInUser;
  @override
  void initState() {

    super.initState();
    getCurrentUser();
  }
  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser;
      if (user != null) {
        loggedInUser = user;
        print(loggedInUser.email);
      }
    } catch (e) {
      print (e);
    }
  }

  // void getMessages() async{
  // final messages = await _firestore.collection('messages').get();
  // for (var message in messages.docs) {
  //   print(message.data().cast());
  // }
  // }
  void messagesStream() async {
    await for( var snapshot in _firestore.collection('bodybuilding').orderBy('timestamp').snapshots()) {
      for (var message in snapshot.docs) {
        print(message.data().cast());
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: null,
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.close),
              onPressed: () {
                messagesStream();
                _auth.signOut();
                Navigator.pop(context);
              }),
        ],
        title: Text('           
  • Related