【问题标题】:Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nulable and 'String' isn't 'Email'+user.email错误:参数类型“字符串?”不能分配给参数类型“字符串”,因为“字符串?”是 nulable 并且 'String' 不是 'Email'+user.email
【发布时间】:2021-06-27 20:28:35
【问题描述】:

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您必须处理以下情况:1) 用户没有个人资料照片,以及 2) 用户没有显示名称。您可以使用三元运算符来处理这个问题:

    @override
    Widget build(BuildContext context) {
      final user = FirebaseAuth.instance.currentUser;
      final String? url = user.photoURL;
      final String? name = user.displayName;
    
      return Container(
        // ...
    
        // Handling the profile picture
        url == null 
          ? SizedBox.shrink()    // If it's missing, display an empty box
          : CircleAvatar(radius: 25, backgroundImage: NetworkImage(url))
        
        // ...
    
        // Handling the display name
        name == null
          ? Text("")    // If it's missing, display an empty text
          ? Text("Name: ${name}", style: TextStyle(color: Colors.white))
      );
    }
    

    【讨论】:

      【解决方案2】:

      您面临的错误来自 null-safety,您发送到 NetworkImage 小部件的参数可以是 nullString,因此您可能需要将代码更新为以下示例:

      NetworkImage(user.photoURL!),
      
      
      Text("Name ${user.displayedName!}", style: TextStyle(color: Colors.white)),
      

      您可以在官方文档中了解有关 null 安全性的更多信息:

      https://flutter.dev/docs/null-safety

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-20
        • 2021-10-21
        • 2021-10-13
        • 2021-09-20
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多