如何在 flutter 中调用 Stream Builder 中的导航器?
How to call navigator inside Stream Builder in flutter?
我有一个问题,当我尝试将导航器 pop() 方法放入字符串生成器中时出现此错误:setState() or markNeedsBuild() called during build
.
我了解到您无法在流生成器中调用导航器,所以有人知道如何解决这个问题吗?
这是我的代码:
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Center(
child: Container(
padding: EdgeInsets.all(15),
color: Colors.white,
child: Column(
children: <Widget>[
SizedBox(height: 55),
SvgPicture.asset('images/svg_example.svg'),
SizedBox(height: 55),
Text("Login App",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black)),
SizedBox(height: 40),
emailField,
SizedBox(height: 45),
passwordField,
SizedBox(height: 45),
loginButton,
SizedBox(height: 15),
StreamBuilder<ApiResponse<LoginResponse>>(
stream: userBloc.authenticationUserStream,
builder: (context,
AsyncSnapshot<ApiResponse<LoginResponse>> snapshot) {
// it will observe changes on the ApiResponse<LoginResponse>
if (!snapshot.hasData) return Container();
switch (snapshot.data.status) {
case Status.LOADING:
return Loading(
loadingMessage: "loading",
);
case Status.COMPLETED:
prefs.saveTokenPref(snapshot.data.data.token);
prefs.saveUserPref(snapshot.data.data.user);
goToMain();
return Container(width: 0.0, height: 0.0);
case Status.ERROR:
// Here you can go to another screen after login success.
return Center(
child: Text("${snapshot.data.message}"),
);
default:
return Container();
}
},
)
],
)),
),
);
}
goToMain() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainScreen()),
);
}
好的,鉴于我的问题有一个 -1,因为有人只是想付出而不真正帮助 post 我把这个问题的答案放在这里:
您只需在 initState()
上执行此操作
这将监听流本身并在 Stream Builder 之外创建 UI 逻辑。
@override
void initState() {
userBloc = UserBloc();
super.initState();
userBloc.userSubject.listen((state) {
if (state.status == Status.COMPLETED) {
goToMain();
}
});
}
我有一个问题,当我尝试将导航器 pop() 方法放入字符串生成器中时出现此错误:setState() or markNeedsBuild() called during build
.
我了解到您无法在流生成器中调用导航器,所以有人知道如何解决这个问题吗?
这是我的代码:
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Center(
child: Container(
padding: EdgeInsets.all(15),
color: Colors.white,
child: Column(
children: <Widget>[
SizedBox(height: 55),
SvgPicture.asset('images/svg_example.svg'),
SizedBox(height: 55),
Text("Login App",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black)),
SizedBox(height: 40),
emailField,
SizedBox(height: 45),
passwordField,
SizedBox(height: 45),
loginButton,
SizedBox(height: 15),
StreamBuilder<ApiResponse<LoginResponse>>(
stream: userBloc.authenticationUserStream,
builder: (context,
AsyncSnapshot<ApiResponse<LoginResponse>> snapshot) {
// it will observe changes on the ApiResponse<LoginResponse>
if (!snapshot.hasData) return Container();
switch (snapshot.data.status) {
case Status.LOADING:
return Loading(
loadingMessage: "loading",
);
case Status.COMPLETED:
prefs.saveTokenPref(snapshot.data.data.token);
prefs.saveUserPref(snapshot.data.data.user);
goToMain();
return Container(width: 0.0, height: 0.0);
case Status.ERROR:
// Here you can go to another screen after login success.
return Center(
child: Text("${snapshot.data.message}"),
);
default:
return Container();
}
},
)
],
)),
),
);
}
goToMain() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainScreen()),
);
}
好的,鉴于我的问题有一个 -1,因为有人只是想付出而不真正帮助 post 我把这个问题的答案放在这里:
您只需在 initState()
上执行此操作
这将监听流本身并在 Stream Builder 之外创建 UI 逻辑。
@override
void initState() {
userBloc = UserBloc();
super.initState();
userBloc.userSubject.listen((state) {
if (state.status == Status.COMPLETED) {
goToMain();
}
});
}