【问题标题】:Type int is not a subtype of type string Flutter Firebaseint 类型不是字符串 Flutter Firebase 类型的子类型
【发布时间】:2021-01-02 23:01:27
【问题描述】:

我正在尝试从 firebase 返回一个 int 数据作为字符串,但我不知道怎么做?我尝试了 toString() 但也许我还需要做更多的事情?

@override
  Widget build(BuildContext context) {
    // <1> Use FutureBuilder
    return FutureBuilder<QuerySnapshot>(
        // <2> Pass `Future<QuerySnapshot>` to future
        future: FirebaseFirestore.instance.collection('UserNames').get(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            // <3> Retrieve `List<DocumentSnapshot>` from snapshot
            final List<DocumentSnapshot> documents = snapshot.data.docs;
            return ListView(
                children: documents
                    .map((doc) => Card(
                          child: ListTile(
                            title: Text(doc['displayName']),
                            subtitle: Text(doc['plastics']),
                          ),
                        ))
                    .toList());
          } else if (snapshot.hasError) {
            return Text('Its Error!');
          }
        });
  }

doc plastics 是错误的来源,因为它是 firestore 中的 int 值。如何将 int 数据转换为文本?谢谢。

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    有两种方法,你无能为力:)

    1. toString()方法

    doc['plastics'].toString()

    1. 字符串插值

    '${doc['plastics']}'

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2019-10-15
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      相关资源
      最近更新 更多