Flutter位置参数报错:AuthStateLoggedOut需1个位置参数却传入0个
问题解决:‘AuthStateLoggedOut.new’需要1个位置参数,但找到0个
问题原因
你的AuthStateLoggedOut类构造函数定义了一个必填的位置参数Exception? e,但你在调用时只传递了exception和isLoading这两个命名参数,完全没有提供这个位置参数,因此触发了参数缺失的错误。而且这个位置参数e在构造函数里没有被使用,属于冗余定义。
修复方案(推荐)
直接删除构造函数里冗余的位置参数,只保留命名参数,这样原有调用代码无需修改即可正常运行:
修改后的AuthStateLoggedOut类代码:
class AuthStateLoggedOut extends AuthState with EquatableMixin{ final Exception? exception; final bool isLoading; // 移除多余的位置参数e const AuthStateLoggedOut({required this.exception, required this.isLoading}); @override List<Object?> get props => [exception, isLoading]; }
备选方案(不推荐)
如果一定要保留位置参数,调用时必须先传递该位置参数,再传命名参数(但此方案无实际意义,因为位置参数未被使用):
修改后的调用代码:
emit(const AuthStateLoggedOut(null, exception: null, isLoading:false));
内容的提问来源于stack exchange,提问作者user22977892
相关产品推荐
相关产品推荐

