【问题标题】:Unhandled Exception: type 'int' is not a subtype of type 'String' in flutter未处理的异常:类型“int”不是颤振中“字符串”类型的子类型
【发布时间】:2021-01-12 12:21:29
【问题描述】:

我无法解决这个问题。 最后我想在主小部件中显示“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中研究了一些文档, 但我无法恢复

【问题讨论】:

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


    【解决方案1】:

    可能doc.data()['count']int,但您需要String。所以打电话给doc.data()['count'].toString()

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

    【讨论】:

    • 感谢您的精彩回答。我可以解决这个问题
    • 这也帮助了我的特殊问题。谢谢!
    【解决方案2】:

    尝试添加 toString() 方法。

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

    【讨论】:

      【解决方案3】:

      你可以用另一种方式来做这个:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-01
        • 2023-04-04
        • 1970-01-01
        • 2021-07-21
        • 2021-09-10
        • 2020-05-24
        • 2021-09-19
        相关资源
        最近更新 更多