未处理的异常:类型“int”不是颤振中“字符串”类型的子类型

时间:2021-01-12 12:21:29

标签: firebase flutter google-cloud-firestore get integer

我无法解决这个问题。 最后我想在主小部件中显示“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();
    });
  }

锻炼.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中的一些文档, 但我无法恢复

4 个答案:

答案 0 :(得分:0)

可能 doc.data()['count'] 是一个 int,但您需要一个 String。所以调用doc.data()['count'].toString()

DocumentSnapshot 未显示,但似乎 dataMap<String, dynamic>

答案 1 :(得分:0)

尝试添加 toString() 方法。

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

答案 2 :(得分:0)

你可以用另一种方式:

int count;
this.count = doc.data()['count'];

显示:child: Text(workout.count.toString(),

答案 3 :(得分:0)

你需要 toString() 方法。 另外,你可以试试

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