【发布时间】: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