Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

'Timestamp' is not a subtype of type 'int' - Flutter - Firebase Firestore

I got an error when I try to get Time set data from Cloud Firestore. I think if I set timestampsInSnapshots: true then the issue will be fixed, but I can't set it because I use cloud_firestore: ^0.16.0 so I couldn't found how can I do this. if I use cloud_firestore: ^0.8.2+1 then I can configure the Firestore's settings. But I wanna set this configuration in version 0.16.0

About Issue:

The following _TypeError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#ec8a0):
type 'Timestamp' is not a subtype of type 'int'

The relevant error-causing widget was
StreamBuilder<QuerySnapshot>
lib/…/main/profile.dart:66
When the exception was thrown, this was the stack
#0      _ProfileState.buildExamHistoryList.<anonymous closure>.<anonymous closure>
lib/…/main/profile.dart:97
#1      MappedListIterable.elementAt (dart:_internal/iterable.dart:411:31)
#2      ListIterator.moveNext (dart:_internal/iterable.dart:340:26)
#3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
...

enter image description here

The stream where I wanna set my data from firestore:

Widget buildExamHistoryList() {
    return StreamBuilder<QuerySnapshot>(
      stream: usersRef.doc(widget.userID).collection('examResults').snapshots(),
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Center(
            child: Text("Something Went Wrong"),
          );
        }
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Container(
              padding: EdgeInsets.only(top: 100),
              child: Center(
                child: SpinKitFadingCircle(
                  color: Colors.black,
                  size: 50,
                ),
              ),
            );
            break;
          default:
            return Column(
              children: [
                ListView(
                  shrinkWrap: true,
                  children: snapshot.data.docs.map((doc) {
                    return Padding(
                      padding: const EdgeInsets.all(10),
                      child: ExamHistoryCard(
                        correctAnswersCount: doc['correctAnswersCount'],
                        incorrectAnswersCount: doc['incorrectAnswersCount'],
                        date: _examHistoryService.readTimestamp(doc['date']),
                      ),
                    );
                  }).toList(),
                ),
              ],
            );
        }
      },
    );
  }

and that is my readTimestamp function:

 String readTimestamp(int timestamp) {
    var now = DateTime.now();
    var format = DateFormat('HH:mm a');
    var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    var diff = now.difference(date);
    var time = '';

    if (diff.inSeconds <= 0 ||
        diff.inSeconds > 0 && diff.inMinutes == 0 ||
        diff.inMinutes > 0 && diff.inHours == 0 ||
        diff.inHours > 0 && diff.inDays == 0) {
      time = format.format(date);
    } else if (diff.inDays > 0 && diff.inDays < 7) {
      if (diff.inDays == 1) {
        time = diff.inDays.toString() + ' Dünen';
      } else {
        time = diff.inDays.toString() + ' Gün ?nce';
      }
    } else {
      if (diff.inDays == 7) {
        time = (diff.inDays / 7).floor().toString() + ' Hefte ?nce';
      } else {
        time = (diff.inDays / 7).floor().toString() + ' Hefte ?nce';
      }
    }

    return time;
  }
question from:https://stackoverflow.com/questions/66063630/timestamp-is-not-a-subtype-of-type-int-flutter-firebase-firestore

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this

String readTimestamp(Timestamp timestamp) {
    var now = DateTime.now();
    var format = DateFormat('HH:mm a');
    var date = timestamp.toDate();
    var diff = now.difference(date);
    var time = '';

    if (diff.inSeconds <= 0 ||
        diff.inSeconds > 0 && diff.inMinutes == 0 ||
        diff.inMinutes > 0 && diff.inHours == 0 ||
        diff.inHours > 0 && diff.inDays == 0) {
      time = format.format(date);
    } else if (diff.inDays > 0 && diff.inDays < 7) {
      if (diff.inDays == 1) {
        time = diff.inDays.toString() + ' Dünen';
      } else {
        time = diff.inDays.toString() + ' Gün ?nce';
      }
    } else {
      if (diff.inDays == 7) {
        time = (diff.inDays / 7).floor().toString() + ' Hefte ?nce';
      } else {
        time = (diff.inDays / 7).floor().toString() + ' Hefte ?nce';
      }
    }

    return time;
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...