#flutter #dart #google-cloud-firestore
#flutter #dart #google-облако-firestore
Вопрос:
Я разрабатываю flutter app.My проект flutter показывает ошибку в конструкторе factory при использовании DocumentSnapshot
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 libmain.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:flutterflutterpackagesflutter_toolsgradleflutter.gradle' line: 896
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:flutterflutterbinflutter.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:
Если вы хотите получить доступ к firebases 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
раздел в официальной документации: