在条件“((snapshot.data ['role'] =='admin')”)之后,页面应导航至adminPage。 home.dart
class Home extends StatelessWidget {
const Home({ Key key, this.user}) : super(key: key);
final FirebaseUser user;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hi! ${user.email}'),
),
body: StreamBuilder<DocumentSnapshot> (
stream: Firestore.instance.collection('users').document(user.uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
switch(snapshot.connectionState) {
case ConnectionState.waiting:return Text('Loading...');
default:
if (snapshot.data['role'] == 'admin') {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => AdminPage(user: user)));
} else {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => MemberPage(user: user)));
}
}
};
},),
);
}
如您所见,我正在使用Navigator.pushReplacement进行导航。 但是,我遇到了这样的错误。
I/flutter (30480): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (30480): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (30480): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (30480): Overlay-[LabeledGlobalKey<OverlayState>#a7637](state: OverlayState#fbfaa(tickers: tracking 4
I/flutter (30480): tickers, entries: [OverlayEntry#1d0eb(opaque: true; maintainState: false),
I/flutter (30480): OverlayEntry#bf6a0(opaque: false; maintainState: true), OverlayEntry#6ab73(opaque: false;
I/flutter (30480): maintainState: false), OverlayEntry#be324(opaque: false; maintainState: true)]))
I/flutter (30480): The widget which was currently being built when the offending call was made was:
I/flutter (30480): StreamBuilder<DocumentSnapshot>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot,
I/flutter (30480): AsyncSnapshot<DocumentSnapshot>>#2644b)
I/flutter (30480):
I/flutter (30480): When the exception was thrown, this was the stack:
I/flutter (30480): #0 Element.markNeedsBuild.<anonymous closure>
package:flutter/…/widgets/framework.dart:3497
I/flutter (30480): #1 Element.markNeedsBuild
package:flutter/…/widgets/framework.dart:3523
I/flutter (30480): #2 State.setState
package:flutter/…/widgets/framework.dart:1138
I/flutter (30480): #3 OverlayState.insertAll
package:flutter/…/widgets/overlay.dart:301
I/flutter (30480): #4 OverlayRoute.install
package:flutter/…/widgets/routes.dart:43
I/flutter (30480): #5 TransitionRoute.install
package:flutter/…/widgets/routes.dart:184
I/flutter (30480): #6 ModalRoute.install
package:flutter/…/widgets/routes.dart:899
I/flutter (30480): #7 NavigatorState.pushReplacement
package:flutter/…/widgets/navigator.dart:1729
I/flutter (30480): #8 Navigator.pushReplacement
package:flutter/…/widgets/navigator.dart:1055
这是我的adminPage。 (请注意,我已经将其导入到我的LoginPage中)。
AdminMainPage.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class AdminPage extends StatefulWidget {
@override
const AdminPage({ Key key, this.user}) : super(key: key);
final FirebaseUser user;
_AdminPageState createState() => _AdminPageState();
}
class _AdminPageState extends State<AdminPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: new Text("Mobile Registration App"),
backgroundColor: Colors.teal,
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
),
);
}
}
我已经浪费了很多时间来解决此问题。 这个问题在home.dart