如何使用 AutomaticKeepAliveClientMixin 重新加载 Flutter StatefulWidget?
下面的代码不会在用户通过 firebase 注销时重新加载 Usermovies 列表 StreamBuilder,而是仅显示旧用户电影数据。
这个 HomeScreen 在底部导航栏中使用 PageView 调用。另一个页面是带有登录和注销按钮的 AccountScreen。
我的问题是如何通过 firebase 在用户注销时重新加载 UserMovies。如何在从 AccountScreen 注销时重新加载 HomeScreen,以便将用户电影流刷新为 null。
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
// need to call super method for AutomaticKeepAliveClientMixin
super.build(context);
print('Rebuild in Home Screen.....');
return StreamBuilder<app.User>(
stream: Provider.of<AuthProvider>(context, listen: true).user,
builder: (context, snapshot) {
if (snapshot.data != null) {
isUserLoggedIn = true;
rebuild = false;
} else if (snapshot.data == null && isUserLoggedIn) {
isUserLoggedIn = false;
rebuild = true;
} else {
isUserLoggedIn = false;
rebuild = false;
}
if (rebuild) {
// Not reloading the Usermovies on user logout, instead showing old user movies data only in the below stream builder
Future.delayed(Duration.zero, () => setState(() {}));
}
return StreamBuilder<List<UserMovies>>(
stream: Provider.of<UserDetailsProvider>(context,
listen: false)
.getUserFavouriteMovies(),
builder: (context, snapshot) {
snapshot.data != null && snapshot.data.length > 0
? print('data there: ')
: print('data zero');
snapshot.data != null && snapshot.data.length > 0
? Scaffold.of(context).showCurrentSnackBar() // to show last favourite movie
: Scaffold.of(context).hideCurrentSnackBar();
return SizedBox(height: 2.0);
},
},
),
}
}