未处理的异常:类型 'int' 不是类型 'String' 的子类型
Unhandled Exception: type 'int' is not a subtype of type 'String' in flutter
我无法解决这个问题。
最后我想在主小部件
中显示 'workout.count'
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;
}
有没有人有想法?
当然我研究了stackflow中的一些文档,
但我无法恢复
可能 doc.data()['count']
是 int
,但您需要 String
。所以调用 doc.data()['count'].toString()
.
DocumentSnapshot
未显示,但 data
似乎是 Map<String, dynamic>
。
尝试添加 toString() 方法。
Padding(
padding: const EdgeInsets.only(top: 80),
child: Text(workout.count.toString(),
style: TextStyle(fontSize: 12)),
);
您可以用另一种方式制作:
int count;
this.count = doc.data()['count'];
显示:child: Text(workout.count.toString(),
我无法解决这个问题。 最后我想在主小部件
中显示 'workout.count'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;
}
有没有人有想法?
当然我研究了stackflow中的一些文档, 但我无法恢复
可能 doc.data()['count']
是 int
,但您需要 String
。所以调用 doc.data()['count'].toString()
.
DocumentSnapshot
未显示,但 data
似乎是 Map<String, dynamic>
。
尝试添加 toString() 方法。
Padding(
padding: const EdgeInsets.only(top: 80),
child: Text(workout.count.toString(),
style: TextStyle(fontSize: 12)),
);
您可以用另一种方式制作:
int count;
this.count = doc.data()['count'];
显示:child: Text(workout.count.toString(),