我有一个问题,当我尝试将 navigator 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()),
);
}