未为类型 'Object' 定义运算符“[]”。尝试为 Flutter 定义运算符 '[]'

The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' for Flutter

我正在尝试将我的数据库值存储到 class 中,但我无法使用 DataSnapshot 将其转换为我的 class。我已经添加了所有必要的空安全运算符。但是还是报错

class User {
  String userID = "";
  String name = "";
  String phoneNo = "";
  String email = "";
  String password = "";

  User(
      {required this.userID,
      required this.name,
      required this.phoneNo,
      required this.email,
      required this.password});

  User.fromSnapshot(DataSnapshot dataSnapshot) {
    userID = dataSnapshot.key!;
    if (dataSnapshot.value != null) {
      name = dataSnapshot.value!["name"] as String;
      email = dataSnapshot.value!['email'];
      phoneNo = dataSnapshot.value!['phone'];
      password = dataSnapshot.value!['password'];
    }
  }
}

我正在尝试将快照值定义为字符串,但也与其他字符串相同。

Error message

尝试

if (dataSnapshot.value != null) {
      final data = dataSnapshot.value as Map;
      name = data["name"] as String;
      email = data['email'] as String;
      phoneNo = data['phone'] as String;
      password = data['password'] as String;
    }

尝试指定您的 DataSnapshot 类型:

  User.fromSnapshot(DataSnapshot<Map<String,dynamic>> dataSnapshot) {
        userID = dataSnapshot.key!;
        if (dataSnapshot.value != null) {
          name = dataSnapshot.value!["name"] as String;
          email = dataSnapshot.value!['email'];
          phoneNo = dataSnapshot.value!['phone'];
          password = dataSnapshot.value!['password'];
        }
      }