我仅通过电子邮件和密码使用Firebase Auth
。登录该应用程序后,抽屉可以正常工作,但是当我单击一个抽屉项目时,再次打开该应用程序(我正在使用无声登录-或使用Firebase方法currentUser
进行自动登录),第二页打开时没有错误,但是当我返回首页时,应用会引发以下错误:
main.dart
home: FutureBuilder<FirebaseUser>(
future: _auth.getCurrentUser(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return Container(
child: Loading.load,
color: Colors.white,
);
default:
if (snapshot.hasError)
return Text('Erro: ${snapshot.error}');
else if (snapshot.data == null)
return PageLogin(
auth: Auth(),
);
else
return PageHome();
}
},
));
auth.dart
class Auth implements BaseAuth {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
static FirebaseUser _user;
Future<FirebaseUser> getCurrentUser() async {
if (_user == null) {
_user = await _firebaseAuth.currentUser();
}
await loadUserAccess();
return _user;
}
}
page_home.dart
class PageHome extends StatelessWidget {
final _scaffoldKey = new GlobalKey<ScaffoldState>();
final Auth auth = Auth();
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: _loadConnections(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container(
child: Loading.load,
color: Colors.white,
);
} else {
return Scaffold(
key: _scaffoldKey,
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: _drawerListItens(context))),
appBar: AppBar(
title: Text("Title"),
body: Container(),
);
}
},
);
}
Future<bool> _loadConnections() async {
try {
if (FirebaseUtil.fbRef() == null) {
await FirebaseUtil.loadReference();
}
//SQLite reference
await DBProvider.db.database;
if (!auth.userIsAdmin()) {
//check if user is admin
await auth.loadUserAccess();
}
return true;
} catch (e) {
return false;
}
}
List<Widget> _drawerListItens(context) {
List<Widget> _drawerItens = List<Widget>();
_drawerItens.addAll([
ListTile(
title: Text("Second Page"),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
SecondPage(String someData)));
},
),
]);
return _drawerItens;
}
}
答案 0 :(得分:0)
重复的键会导致此错误。在我的应用中,正是Scaffold的Class BaseScaffold{
final GlobalKey<ScaffoldState> _scaffoldKey;
BaseScaffold(this._scaffoldKey);
Widget build(BuildContext context) {
return Scaffold(
key : _scaffoldKey
...
);
}
}
Class B extends StatelessWidget{
final _scaffoldKey = GlobalKey<ScaffoldState>();
Widget build(BuildContext context) {
return BaseScaffold(
key: _scaffoldKey
...
);
}
}
导致了此错误。
如果您为Scaffold和AppBar使用基类,则从调用基类的类中传递键。例如
{{1}}