【问题标题】:Easiest Way to overcome operator assignment in dart克服飞镖中运算符分配的最简单方法
【发布时间】:2020-09-12 08:17:22
【问题描述】:

我正在开发一个颤振应用程序。当我使用 DocumentSnapshot 时,我的颤振项目在工厂构造函数中显示错误

用户.dart

import 'package:cloud_firestore/cloud_firestore.dart';

class User {
  final String id;
  final String username;
  final String email;
  final String photoUrl;
  final String displayname;
  final String bio;

  User({
   this.id,
   this.username,
   this.email,
   this.photoUrl,
   this.displayname,
   this.bio
  });

  factory User.fromDoc(DocumentSnapshot doc){
    return User(
      id: doc['id'],
      username: doc['username'],
      email: doc['email'],
      photoUrl: doc['photoUrl'],
      displayname: doc['displayName'],
      bio: doc['bio'],
    );
  }
}

错误日志

Launching lib\main.dart on sdk gphone x86 arm in debug mode...
Running Gradle task 'assembleDebug'...
lib/models/user.dart:22:14: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
      id: doc['id'],
             ^
lib/models/user.dart:23:20: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
      username: doc['username'],
                   ^
lib/models/user.dart:24:17: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
      email: doc['email'],
                ^
lib/models/user.dart:25:20: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
      photoUrl: doc['photoUrl'],
                   ^
lib/models/user.dart:26:23: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
      displayname: doc['displayName'],
                      ^
lib/models/user.dart:27:15: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
      bio: doc['bio'],
              ^


FAILURE: Build failed with an exception.

* Where:
Script 'C:\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 896

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 7s
Exception: Gradle task assembleDebug failed with exit code 1

由于我必须在应用程序的各个部分中使用 DocumentSnapshot,因此有什么方法可以解决此操作员问题?如果有任何永久的解决方案可以使用这种方法?

谢谢

【问题讨论】:

  • 如果您认为答案有用,请留下评论为什么是或为什么不是。或者将答案标记为正确,以便其他人可以从中获利!

标签: flutter dart google-cloud-firestore


【解决方案1】:

如果您想访问 Firebase DocumentSnapshot,您需要访问其数据属性 snapshot.data。如果你想访问它背后的地图,请使用snapshot.data.data

所以在你的例子中是这样的:

  factory User.fromDoc(DocumentSnapshot doc){
    return User(
      id: doc.data.data['id'],
      username: doc.data.data['username'],
      email: doc.data.data['email'],
      photoUrl: doc.data.data['photoUrl'],
      displayname: doc.data.data['displayName'],
      bio: doc.data.data['bio'],
    );
  }
}

请参阅官方文档中的One-Time Read 部分:

https://firebase.flutter.dev/docs/firestore/usage/

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2019-10-24
    • 2013-07-10
    • 2020-03-03
    • 2021-02-27
    相关资源
    最近更新 更多