【问题标题】:Fail to use Provider and Get Data: Tried to call Provider.of<dynamic>无法使用提供者和获取数据:尝试调用 Provider.of<dynamic>
【发布时间】:2021-01-07 23:13:02
【问题描述】:

我无法通过 Provider 从 firestore/firebase 检索布尔值。

图片中的错误信息:

getter 低于问题的来源:

    _getProfileData(AuthNotifier authNotifier) async {
      final uid = await Provider.of<AuthNotifier>(context, listen: false).getCurrentUID();
    await Provider.of(context, listen: false)
        .collection('Users')
        .document(uid)
        .get().then((result) {
          user.isAdmin = result.data['isAdmin'];
    });
  }]

使用来自提供者的数据的未来构建器。

future: _getProfileData(authNotifier),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              _isAdmin = snapshot.data['isAdmin'] ?? false;
            }
            return Container(
              child: Column(
                children: <Widget>[
                  adminFeature(),
                  ]

下面,我有身份验证通知程序可以从 firebase 检索数据。

  Future<String> getCurrentUID() async {
    return (await _firebaseAuth.currentUser()).uid;
  }

  // GET CURRENT USER
  Future getCurrentUser() async {
    return await _firebaseAuth.currentUser();
  }
class User {
  List favorites = [];  
  String documentID;
  String displayName;
  String email;
  String password;
  bool isAdmin;

  User({
    this.favorites,
    this.documentID,
    this.displayName, 
    this.email,
    this.password,
    this.isAdmin,

    });

  factory User.fromFirestore(DocumentSnapshot document) {
    Map data = document.data;

    return User(
      favorites: data['favorite'] ?? [],
      documentID: document.documentID,
      displayName: data['displayName'] ?? '',
      email: data['email'] ?? '',
      isAdmin: data['isAdmin'] ?? false,
    );
  }

  // get admin => null;
    Map<String, dynamic> toMap() {
    return {
      'displayName': displayName,
      'email': email,
      'isAdmin': isAdmin,
    };
}
}

【问题讨论】:

  • 查看数据结构会很有帮助,因为您可能没有检索到所需的布尔值。
  • 嗨 Andrew,我为用户添加了结构。

标签: firebase flutter google-cloud-firestore provider


【解决方案1】:
await Provider.of(context, listen: false) //<--- here is your problem, you don't define the type of Provider
   .collection('Users')
   .document(uid)
   .get().then((result) {
     user.isAdmin = result.data['isAdmin'];
});

改成这样的

await Provider.of<FirebaseFirestore>(context, listen: false)...the rest of your code...

【讨论】:

  • 谢谢,我在此之前尝试过,但是,没有为“用户”类型定义“收集”方法。尝试将名称更正为现有方法的名称,或定义名为“collection”的方法
  • 输入用户?方法集合是 FirebaseFirestore 的,你在哪里使用或声明提供者?
  • 此外,您未来的构建器还期望一些 map 类型的数据 (snapshot.data['isAdmin]) 但您的方法从不返回任何内容,而不是等待提供者,您应该返回提供者值或其他东西
  • 嗨,EdwynZN,应该在 authNotifier 中完成。
  • 该错误与发布的错误无关,更新错误发生的位置以及您更改了什么以出现此新错误
【解决方案2】:

我相信您需要使用 T 的用户,而不仅仅是用户:

return User<T>( // <--- Here
      favorites: data['favorite'] ?? [],
      documentID: document.documentID,
      displayName: data['displayName'] ?? '',
      email: data['email'] ?? '',
      isAdmin: data['isAdmin'] ?? false,
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多