【问题标题】:Firebase/Flutter: Compiler reads map<String, dynamic> as map<dynamic, dynamic> can't solve it :/Firebase/Flutter:编译器将 map<String, dynamic> 读取为 map<dynamic, dynamic> 无法解决:/
【发布时间】:2021-07-14 09:30:00
【问题描述】:

我正在使用 Firebase 创建身份验证服务,并且我有 UserModel 来处理用户数据,我有一个带有函数的类:

class UserModel extends ChangeNotifier {
  late String uid;
  late String username;
  late String email;
  late String password;
  late String type;

  UserModel.empty();

  UserModel(
      String uid, String username, String email, String password, String type) {
    this.uid = uid;
    this.username = username;
    this.email = email;
    this.password = password;
    this.type = type;
     }

  Map<String, dynamic> toMap(UserModel user) {
    var data = Map<String, dynamic>();

    data["uid"] = user.uid;
    data["nickname"] = user.username;
    data["email"] = user.email;
    data["password"] = user.password;
    data["type"] = user.type;

    return data;
  }
}

还有一个我有验证功能的类:

class AuthenticationService {
  final FirebaseAuth _firebaseAuth;
  UserModel userModel = UserModel.empty();
  final userRef = FirebaseFirestore.instance.collection('users');

  AuthenticationService(this._firebaseAuth);

  Stream<User?> get authStateChanges => _firebaseAuth.authStateChanges();

  Future<void> addUserToDB(
      {required String uid,
      required String username,
      required String email,
      required String password,
      required String type}) async {
    userModel = UserModel(uid, username, email, password, type);
    var firebaseUser = FirebaseAuth.instance.currentUser;
    await userRef.doc(uid).set(userModel.toMap(userModel));
  }
}

我有一个错误:

参数类型“Map”不能分配给参数类型“Map”。

有什么办法解决吗?

我暂时不使用UserModel,但我真的很想修复它。

【问题讨论】:

  • 尝试将toMap方法的返回类型改为Map&lt;dynamic, dynamic&gt;

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

案例是我拼错了一个导入,它正在读取我的旧UserModel:D

UserModel 必须是 map&lt;String,dynamic&gt; 因为 set 函数被告知它不能与 map&lt;dynamic,dynamic&gt; 一起使用。

所以上面的代码很好,对我有用。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 2021-09-01
    • 2021-02-11
    • 2021-04-13
    • 2021-03-27
    • 2021-06-09
    • 1970-01-01
    • 2020-01-10
    • 2018-10-14
    相关资源
    最近更新 更多