The below code works fine but doesn't hide snack bar on user signout.
It invokes the code _scaffoldKey.currentState.hideCurrentSnackBar();
but doesn't hide the snackbar.
How to remove or hide the current snackbar?
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) {
return StreamBuilder<List<UserMovies>>(
stream: Provider.of<UserDetailsProvider>(context, listen: false)
.getUserFavouriteMovies(),
builder: (context, snapshotMovies) {
Movies movies = snapshotMovies.hasData &&
snapshotMovies.data.length > 0
? snapshotMovies.data[0]
: null;
_showSnackBar(context, movies);
return SizedBox(height: 2.0);
},
},
),
}
void _showSnackBar(BuildContext ctx, Movies movies) {
if (movies == null) {
_scaffoldKey.currentState.hideCurrentSnackBar();
} else {
WidgetsBinding.instance.addPostFrameCallback(
(_) => {
print('showCurrentSnackBar'),
_scaffoldKey.currentState.showSnackBar(
SnackBar(
backgroundColor: Colors.deepPurple.shade300,
content: Text("Hello World!"),
duration: const Duration(
days: 1,
),
behavior: SnackBarBehavior.floating,
margin: EdgeInsets.zero,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
const Radius.circular(0.0),
),
),
),
),
},
);
}
}
}