【问题标题】:The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' error没有为“对象”类型定义运算符“[]”。尝试定义运算符 '[]' 错误
【发布时间】:2021-05-11 11:43:42
【问题描述】:
body: Center(
    child: Container(
      height: 300,
      width: 300,
      //color: Colors.black,
      child: StreamBuilder<QuerySnapshot>(
        stream: firestore.collection("users").snapshots(),
        builder: (context, snapshot) {
          if (snapshot == ConnectionState.waiting) {
            return CircularProgressIndicator();
          } else {
            return new ListView(
              children: snapshot.data!.docs.map((DocumentSnapshot documentSnapshot){
                return Card(
                  child: Text(documentSnapshot.data()!["Name"]),
                );
              }).toList(),
            );
          }
        },
      ),
    ),
  ),

["Name"] 显示错误未为类型“Object”定义运算符“[]”。尝试定义运算符“[]”。

【问题讨论】:

标签: flutter dart


【解决方案1】:

您需要将documentSnapshot.data() 转换为DocumentSnapshot 正确类型:

(documentSnapshot.data() as DocumentSnapshot)["Name"]

【讨论】:

    【解决方案2】:

    你需要像这样投射:

    (documentSnapshot.data() as Map<String, dynamic>)['Name']
    

    【讨论】:

    • 这对我有用。
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 2021-10-07
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多