在飞镖中克服操作员分配的最简单方法

时间:2020-09-12 08:17:22

标签: flutter dart google-cloud-firestore

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

User.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,是否有任何方法可以解决此操作员问题?如果有永久性解决方案可以使用这种方法?

谢谢

1 个答案:

答案 0 :(得分:0)

如果要访问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/