skip to Main Content

I wrote a timestamp field to a record. I now want to use it in my Flutter app, but I’m getting this error:

NoSuchMethodError: Class ‘Timestamp’ has no instance method ‘getSeconds’.

Receiver: Instance of ‘Timestamp’

Tried calling: getSeconds()

final docRef = firestore.collection(usersCollection).doc(currentUID).collection(friendsCollection).doc('a6b5Ob3TqOYPHOqssype');

final returnData = await docRef.get();
final time = returnData.get('timestamp');
print('##MyApp## database test returnData: ' + time.getSeconds());

2

Answers


  1. The Timestamp class has no getSeconds method.

    Instead, use the seconds property:

    final time = returnData.get('timestamp');
    print('##MyApp## database test returnData: ' + time.seconds);
    

    or convert it to a DateTime

    final time = returnData.get('timestamp').toDate();
    print('##MyApp## database test returnData: ' + time.second);
    
    Login or Signup to reply.
  2. NoSuchMethodError: Class ‘Timestamp’ has no instance method ‘getSeconds’.

    I assume you are facing this by referring to very old article/code. And now in dart, It is not preffered to use getters and setters as it uses implicitly.

    To back my point.

    Refer pixels elephant’s answer

    Instance variables in Dart have implicit getters and setters. So for your example code, it will operate in exactly the same way, since all you have done is changed from an implicit getter and setter to an explicit getter and setter.


    1. To solve the error use seconds instead of getSeconds()

      • So now when you call time.seconds internally it is calling
        int get seconds => _seconds;
        
    2. This worked, but why doesn’t the intellisense show seconds as an option when I do time ?

      Try defining the type:

      DateTime time = returnData.get('timestamp').toDate();
      print('##MyApp## database test returnData: ' + time.second);
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search