“用户”类没有默认构造函数-flutter
Posted
技术标签:
【中文标题】“用户”类没有默认构造函数-flutter【英文标题】:The class 'User' doesn't have a default constructor -flutter 【发布时间】:2022-01-24 01:47:10 【问题描述】:我的颤振应用有问题。我正在关注“The Net Ninja”的flutter-firebase教程,但我添加了“?”在“用户”之后使其可以为空。
但是,我仍然在 main.dart 中的 'AuthService().user' 第 10 行收到此错误:'参数类型 'Stream
'User' 第 16 行的 auth.dart 中出现错误:'User' 类没有默认构造函数。
我尝试添加和删除“?” '>' 之前和之后的所有地方,但这没有帮助。 我也不能添加“?”在第 16 行的“用户”之后,因为它在“uid”上给出了错误。
这是我的 main.dart:
import 'package:flutter/material.dart';
import 'package:irrigationapp/screens/authenticate/sign_in.dart';
import 'package:irrigationapp/screens/wrapper.dart';
import 'package:irrigationapp/services/auth.dart';
import 'package:irrigationapp/models/user.dart';
import 'screens/language.dart';
import 'package:provider/provider.dart';
void main() => runApp(StreamProvider<User?>.value(
value: AuthService().user,
initialData: null,
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Wrapper()
),
));
这是我的 auth.dart:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:irrigationapp/models/user.dart' as UserModal;
import 'package:irrigationapp/services/database.dart';
class AuthService
final FirebaseAuth _auth = FirebaseAuth.instance;
//create a user object based on FirebaseUser (the return type is user then)
//underscore cz this is a private function that we can only use here.
//if it's true return the uid, else return null.
// ignore: unused_element
User? _userFromFirebaseUser(User? user)
// ignore: unnecessary_null_comparison
if (user !=null)
return User?(uid: user.uid);
else
return null;
//auth change user stream
Stream<User?>? get user
return _auth.authStateChanges()
.map(_userFromFirebaseUser);
//sign in anonymously
Future signInAnon() async
try
UserCredential result = await _auth.signInAnonymously();
User? user = result.user;
return _userFromFirebaseUser(user!);
catch(e)
// ignore: avoid_print
print(e.toString());
return null;
//sign in using mail and pass
Future signInWithEmailAndPassword(String email, String password) async
try
UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
// ignore: unused_local_variable
User? user = result.user;
return _userFromFirebaseUser(user!);
catch(e)
// ignore: avoid_print
print(e.toString());
return null;
感谢您的帮助
【问题讨论】:
【参考方案1】:package:firebase_auth
renamed its FirebaseUser
class to just User
。大概您尝试通过搜索并替换您的代码以将FirebaseUser
更改为User
来解决此问题,但这与您自己的User
类产生了名称冲突。然后,您尝试通过为您的类添加库前缀来避免这种冲突,但没有正确修复所有使用站点。
(The Firebase User
class 没有提供任何公共构造函数,从而捕获了错误。)
你有一个函数:
//create a user object based on FirebaseUser (the return type is user then)
//underscore cz this is a private function that we can only use here.
//if it's true return the uid, else return null.
// ignore: unused_element
User? _userFromFirebaseUser(User? user)
// ignore: unnecessary_null_comparison
if (user !=null)
return User(uid: user.uid);
else
return null;
目前没有意义,因为它将User
作为参数,然后返回相同的类型。函数名称和描述表明您打算接受一个 Firebase User
对象并返回您自己的 User
对象,大概来自您以 UserModel
为前缀的 package:irrigationapp/models/user.dart
。因此,您需要将其更改为:
//create a user object based on FirebaseUser (the return type is user then)
//underscore cz this is a private function that we can only use here.
//if it's true return the uid, else return null.
// ignore: unused_element
UserModel.User? _userFromFirebaseUser(User? user)
// ignore: unnecessary_null_comparison
if (user !=null)
return UserModel.User(uid: user.uid);
else
return null;
(不过,我个人认为最好为 Firebase 类添加库前缀。)
【讨论】:
以上是关于“用户”类没有默认构造函数-flutter的主要内容,如果未能解决你的问题,请参考以下文章