我建立了一个简单的登录页面,单击该按钮可打开菜单以选择一个Google帐户,然后导航到应用程序的主页。
单击该按钮,但未弹出登录菜单。它抛出一个错误,说:
E/flutter ( 4974): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator. E/flutter ( 4974): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
我尝试将新上下文添加到树中,但是随后出现了另一个错误:
E/flutter (22865): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null. E/flutter (22865): Receiver: null E/flutter (22865): Tried calling: findAncestorStateOfType()
我该怎么办?
这是我的代码:
import 'package:flutter/material.dart';
import 'package:sorted/screens/sortedMain.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
class LoginMain extends StatefulWidget {
LoginMain({Key key}) : super(key: key);
@override
_LoginMainState createState() => _LoginMainState();
}
class _LoginMainState extends State<LoginMain> {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
FirebaseUser _user;
bool isUserSignedIn = false;
@override
void initState() {
super.initState();
checkIfUserIsSignedIn();
}
void checkIfUserIsSignedIn() async {
var userSignedIn = await _googleSignIn.isSignedIn();
setState(() {
isUserSignedIn = userSignedIn;
});
}
Future<FirebaseUser> _handleSignIn() async {
FirebaseUser user;
bool userSignedIn = await _googleSignIn.isSignedIn();
setState(() {
isUserSignedIn = userSignedIn;
});
if (isUserSignedIn) {
user = await _auth.currentUser();
} else {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
user = (await _auth.signInWithCredential(credential)).user;
userSignedIn = await _googleSignIn.isSignedIn();
setState(() {
isUserSignedIn = userSignedIn;
});
}
_user = user;
return user;
}
void onGoogleSignIn(BuildContext context) async {
FirebaseUser user = await _handleSignIn().then((value) => null);
BuildContext context;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SortedMain(_googleSignIn, user)),
);
setState(() {
isUserSignedIn == null ? true : false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Sorted.'),
backgroundColor: Color(0xff0A3D62),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
const Color(0xFF5761B2),
const Color(0xFF1FC588),
])),
),
),
drawer: null,
body: Container(
padding: EdgeInsets.only(top: 30),
width: 360,
height: 600,
color: const Color(0xFF273748),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 300,
height: 300,
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.white, Colors.grey[400]]),
),
child: MaterialButton(
height: 50.0,
minWidth: 150.0,
color: Colors.green,
splashColor: Colors.teal,
textColor: Colors.white,
child: new Icon(Icons.arrow_forward),
onPressed: () {
BuildContext context;
onGoogleSignIn(context);
},
)),
],
)),
),
),
);
}
}
答案 0 :(得分:0)
尝试使用以下代码来设置Firebase类
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
class AuthProvider {
final FirebaseAuth _auth = FirebaseAuth.instance;
User _userFromFirebaseUser(FirebaseUser user) {
return user != null ? User(uid: user.uid) : null;
}
Future<bool> loginWithGoogle() async {
try {
GoogleSignIn googleSignIn = GoogleSignIn();
GoogleSignInAccount account = await googleSignIn.signIn();
if (account == null) return false;
AuthResult res =
await _auth.signInWithCredential(GoogleAuthProvider.getCredential(
idToken: (await account.authentication).idToken,
accessToken: (await account.authentication).accessToken,
));
if (res.user == null) return false;
return true;
} catch (e) {
print("Error logging with google");
return false;
}
}
Future<void> logOut() async {
try {
await _auth.signOut();
} catch (e) {
print("error logging out");
}
}
}
class User {
final String uid;
User({ this.uid });
}
if
语句限制您的导航器,以便在未完成登录过程的情况下无法导航。
CustomButtom(
onpressed : () async {
bool res = await AuthProvider().loginWithGoogle();
if (res) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SortedMain()));
} else
print("Login failed");
},
),