首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数类型为“Bro?Function(Bro?)”无法指定参数类型'Bro? Function(用户?)‘

参数类型为“Bro?Function(Bro?)”无法指定参数类型'Bro? Function(用户?)‘
EN

Stack Overflow用户
提问于 2022-09-21 11:04:31
回答 1查看 31关注 0票数 0

auth.dart

代码语言:javascript
复制
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:lewandowski/models/bro.dart';

import '../shared/database.dart';

class AuthService{
  // method to sign in anon
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Bro? get bro => null;
  // create an object based on firebaseuser
  Bro? _userfromFirebaseUser(Bro? user){
    return User != null ? Bro(uid:user!.uid) : null;
  }

// change user stream

  Stream<Bro?> get user {
    return _auth.authStateChanges().map(_userfromFirebaseUser);
  }
  Future signAnon() async {
    try {
      UserCredential result = await _auth.signInAnonymously();
      User? user = result.user;
      return user;
    }
    catch (e) {
      print(e.toString());

      return null;
    }
  }


  Future signInWithEmailandPassword(String email, String password) async{
    try {
      UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      User? user = result.user;
      return _userfromFirebaseUser(user as Bro);
    }
    catch(e){
      print(e.toString());
    }
  }// method to sign in with email and password

  // method to sign up with email and password
  Future registerWithEmailandPassword(String email, String password) async{
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);
      User? user = result.user;
      // use of brewscollection
      await DatabaseService(uid: user!.uid).updateDatabase('0', 'new user', 100);
      return _userfromFirebaseUser(user as Bro);
    }
    catch(e){
      print(e.toString());
    }
  }

  //method to sign out
Future signOut() async{
    try{
      return await _auth.signOut();
    }
    catch(e){
      print(e.toString());
    }
    return null;
}

}

以下错误:

代码语言:javascript
复制
Stream<Bro?> get user {
        return _auth.authStateChanges().map(_userfromFirebaseUser);
says:The argument type 'Bro? Function(Bro?)' can't be assigned to the parameter type 'Bro? Function(User?)'.
Running Gradle task 'assembleDebug'...
lib/services/auth.dart:20:41: Error: The argument type 'Bro? Function(Bro?)' can't be assigned to the parameter type 'Bro? Function(User?)'.
 - 'Bro' is from 'package:lewandowski/models/bro.dart' ('lib/models/bro.dart').
 - 'User' is from 'package:firebase_auth/firebase_auth.dart' ('../flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-3.9.0/lib/firebase_auth.dart').
    return _auth.authStateChanges().map(_userfromFirebaseUser);
                                        ^


FAILURE: Build failed with an exception.
EN

回答 1

Stack Overflow用户

发布于 2022-09-21 11:09:07

来自_auth.authStateChanges()的值将是User,但您期待的是Bro

尝试将函数参数类型从Bro?更改为User?

代码语言:javascript
复制
 Bro? _userfromFirebaseUser(User? user){
    return user != null ? Bro(uid:user!.uid) : null;
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73799687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档