【问题标题】:Unable to access the fields of Firebase Firestore doc in flutter [duplicate]无法在flutter中访问Firebase Firestore doc的字段[重复]
【发布时间】: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
    );
  }
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    不用.data()试试这个

    factory TeamModel.fromDocument(DocumentSnapshot doc)
      {
        return TeamModel(
          id: doc.id,
          name: doc['name'],
          url: doc['url'],
          category: doc['category'],
          role: doc['role'],
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2020-02-20
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 2018-11-03
      相关资源
      最近更新 更多