gpt4 book ai didi

flutter - 在 Dart 中克服运算符(operator)分配的最简单方法

转载 作者:行者123 更新时间:2023-12-03 03:49:57 28 4
gpt4 key购买 nike

我正在开发一个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,所以有什么方法可以纠正此运算符(operator)问题吗?是否有可用的永久解决方案来使用这种方法?
谢谢

最佳答案

如果要访问Firebase DocumentSnapshot,则需要访问其数据属性snapshot.data。如果要访问其后面的 map ,请使用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/

关于flutter - 在 Dart 中克服运算符(operator)分配的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63858315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com