【问题标题】:_TypeError (type 'Null' is not a subtype of type 'FutureOr<String>')_TypeError(类型 \'Null\' 不是类型 \'FutureOr<String>\' 的子类型)
【发布时间】:2022-11-14 05:27:04
【问题描述】:

我正在尝试从 Firestore 获取个人资料图片。但它给出了一个错误。 这是用于从数据库中获取图像的功能代码。如果可以,请提供帮助

Future<String> getUserImage() async {
  final uid = auth.currentUser?.uid;
  final users = await firestore
     .collection("app")
     .doc("user")
     .collection("driver")
     .doc(uid)
     .get();
  return users.data()?['dp'];
}

【问题讨论】:

    标签: flutter dart google-cloud-firestore dart-null-safety


    【解决方案1】:

    你的getUserImage方法不能返回null,你可以返回默认值 return users.get('dp')?? "got null";

    或接受可为空的数据

    Future<String?> getUserImage() async {
      final uid = auth.currentUser?.uid;
      final users = await firestore
         .collection("app")
         .doc("user")
         .collection("driver")
         .doc(uid)
         .get();
      return users.get('dp');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2021-12-14
      • 2021-10-15
      • 2021-02-05
      • 1970-01-01
      • 2023-02-24
      • 2020-11-26
      相关资源
      最近更新 更多