颤振异常:消费者中应具有类型 '(() => void)?' 的值



我的登录屏有以下功能:

loginUIController() {
return Consumer<ControllerLogin>(builder: (context, model, child) {
// if user are already login
if (model.userModel != null) {
return Center(
// show user details
child: alreadyLoggedInScreen(model),
);
} else {
// if user not login
return notLoginScreen(model);
}
});
}
notLoginScreen(ControllerLogin model) {
return Center(
child: Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: Image.asset('assets/googleback.jpg'),
),
GestureDetector(
onTap: () {
Provider.of<ControllerLogin>(context, listen: false).allowUserToLogin();
},
child: Image.asset(
'assets/google.png',
width: 250,
),
)
],
),
);
}

构造器是:

Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: loginUIController(),
);
}

notLoginScreen显示OK,但每次我点击Login按钮,日志显示一个异常:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following TypeErrorImpl was thrown building Consumer<ControllerLogin>(dirty, dependencies:
[_InheritedProviderScope<ControllerLogin?>]):
Expected a value of type '(() => void)?', but got one of type '_Future<dynamic>'
The relevant error-causing widget was:
Consumer<ControllerLogin>
Consumer:file:///E:/CoderLife/Learning/flutter-learning/google_signin_authentication/lib/loginScreen.dart:17:12

ControllerLogin是:

class ControllerLogin with ChangeNotifier {
FirebaseAuth auth = FirebaseAuth.instance;
var googleSignIn = GoogleSignIn();
GoogleSignInAccount? googleSignInAccount;
UserCredential? userCredential;
UserModel? userModel;
User? user;
allowUserToLogin() async {
if (kIsWeb) {
GoogleAuthProvider authProvider = GoogleAuthProvider();
try {
userCredential = await auth.signInWithPopup(authProvider);
user = userCredential!.user;
} catch (e) {
print(e);
}
} else {
googleSignInAccount = await googleSignIn.signIn();
if (googleSignInAccount != null) {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount!.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
try {
userCredential = await auth.signInWithCredential(credential);
user = userCredential!.user;
} on FirebaseAuthException catch (e) {
if (e.code == 'account-exists-with-different-credential') {
print(e);
} else if (e.code == 'invalid-credential') {
print(e);
}
} catch (e) {
print(e);
}
}
}
if (user != null) {
userModel = new UserModel(
displayName: user!.displayName,
email: user!.email,
photoUrl: user!.photoURL);
print(userModel!.toJson());
notifyListeners();
}
}
allowUserToLogOut() async {
this.googleSignInAccount = await googleSignIn.signOut();
userModel = null;
notifyListeners();
}
}

Provider使用的构建函数如下:

Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => ControllerLogin()),
],
child: MaterialApp(
title: 'Google Login',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FutureBuilder(
future: _initApp,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
print('error');
}
if (snapshot.connectionState == ConnectionState.done) {
return LoginScreen();
}
return CircularProgressIndicator();
},
),
),
);
}

我已经把onTap作为一个async函数,但它没有工作。

似乎我在消费方面做错了什么。有人能帮我吗?

不要像这样使用consumer use Provider

model =
Provider.of<ControllerLogin>(context, listen: false).allowUserToLogin();

最新更新