很明显,我在这里做的是根本错误的事情...登录后,我将新的bool值(T)发送到isAuthorized流。 StreamBuilder重新运行并最终执行正确的if-else分支,但是由于某种原因,登录小部件仍显示在屏幕上?
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
theme: appTheme,
initialRoute: '/',
home: StreamBuilder<bool>(
stream: getIt.get<SessionBloc>().isAuthorized,
initialData: false,
builder: (context, snapshot) {
if (snapshot.data) {
return Home();
} else {
return Login();
}
},
),
routes: {
'/': (context) => Home(),
'/profile': (context) => Profile(),
'/login': (context) => Login(),
'/register': (context) => Register(),
},
),
);
}
}
答案 0 :(得分:0)
因为我不知道您的集团在做什么,所以我创建了一个模拟集团的简单流。此代码段正常工作。如果集团为您提供了所需的东西,并且进行了if-else阻止,那么您应该考虑删除'/': (context) => Home()
。
这是我在家时遇到的例外: 如果指定了home属性,则路由表不能包含“ /”条目,因为它是多余的。
class Authorization {
final _authorization = StreamController<bool>();
StreamSink<bool> get send => _authorization.sink;
Stream<bool> get isAuthorized => _authorization.stream;
get close => _authorization.close();
}
class MyApp extends StatelessWidget {
Authorization a = new Authorization();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
home: StreamBuilder<bool>(
stream: a.isAuthorized,
initialData: false,
builder: (context, snapshot) {
if (snapshot.data) {
return Home(auth: a);
} else {
return Login(auth: a);
}
},
),
routes: {
'/login': (context) => Login(auth: a),
},
);
}
}
class Home extends StatelessWidget {
final Authorization _auth;
Home({@required Authorization auth})
: assert(auth != null),
_auth = auth;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[Text("Home"), RaisedButton(
onPressed: () {
_auth.send.add(false);
},
)],
),
);
}
}
class Login extends StatelessWidget {
final Authorization _auth;
Login({@required Authorization auth})
: assert(auth != null),
_auth = auth;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[Text("Login"), RaisedButton(
onPressed: () {
_auth.send.add(true);
},
)],
),
);
}
}