Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

firebase - Unhandled Exception: type 'int' is not a subtype of type 'String' in flutter

I cannot resolve this problem. Finally I want to display 'workout.count' in main widget

main.dart

Padding(
                      padding: const EdgeInsets.only(top: 80),
                      child: Text(workout.count,
                          style: TextStyle(fontSize: 12)),
                    )

main_model

void getWorkoutListRealtime() {
    final snapshots =
    FirebaseFirestore.instance.collection('workoutlist').snapshots();
    snapshots.listen((snapshot) {
      final docs = snapshot.docs;
      final workoutlist = docs.map((doc) => Workout(doc)).toList();
      workoutlist.sort((a, b) => b.createdAt.compareTo(a.createdAt));
      this.workoutlist = workoutlist;
      notifyListeners();
    });
  }

workout.dart

class Workout {

  Workout(DocumentSnapshot doc) {

    this.documentReference = doc.reference;

    this.title = doc.data()['title'];
    this.count = doc.data()['count'];
    final Timestamp timestamp = doc.data()['createdAt'];
    this.createdAt = timestamp.toDate();
  }

  String title;
  String count;
  DateTime createdAt;
  bool isDone = false;
  DocumentReference documentReference;

}

Does anyone have ideas?

Of course I research some documents in stackflow, but I cannot recover


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

1 Answer

0 votes
by (71.8m points)

Probably doc.data()['count'] is an int but you need a String. So call doc.data()['count'].toString().

DocumentSnapshot is not shown but it seems data is a Map<String, dynamic>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.5k users

...