无法在flutter中访问Firebase Firestore doc的字段[重复]
Posted
技术标签:
【中文标题】无法在flutter中访问Firebase Firestore doc的字段[重复]【英文标题】:Unable to access the fields of Firebase Firestore doc in flutter [duplicate] 【发布时间】:2021-08-06 10:10:28 【问题描述】:我想访问文档字段,为此我使用以下代码,但出现错误:-
The operator '[]' isn't defined for the type 'Object'. (Documentation)
我的 Cloud Firestore 版本是 cloud_firestore: ^2.1.0
我的代码:-
import 'package:cloud_firestore/cloud_firestore.dart';
class TeamModel
final String id;
final String name;
final String url;
final String category;
final String role;
TeamModel(
this.id,
this.name,
this.url,
this.category,
this.role
);
factory TeamModel.fromDocument(DocumentSnapshot doc)
return TeamModel(
id: doc.id,
name: doc.data()['name'],// I am getting the error on this ['name']
url: doc.data()['url'], // same error here
category: doc.data()['category'],// same error here
role: doc.data()['role'],// same error here
);
【问题讨论】:
【参考方案1】:不用.data()
试试这个
factory TeamModel.fromDocument(DocumentSnapshot doc)
return TeamModel(
id: doc.id,
name: doc['name'],
url: doc['url'],
category: doc['category'],
role: doc['role'],
);
【讨论】:
以上是关于无法在flutter中访问Firebase Firestore doc的字段[重复]的主要内容,如果未能解决你的问题,请参考以下文章
在 Flutter 中捕获云 Firestore 文档快照错误
无法在flutter中访问Firebase Firestore doc的字段[重复]
Flutter 项目中的 Application.kt 给出错误 - 无法访问“com.google.firebase.messaging.zzf”