Error: "Bad state: No element" when using BLoC_pattern with a bool value
9,047
Solution 1
Verify your imports in main.dart
Solution 2
I had the same issue when trying to access the first element of an empty iterable.
@override
Widget build(BuildContext context) {
return BlocProvider<MyBloc>(
create: (context) {
var list = myIterable.keys.first; // keys was empty
var myBloc = MyBloc();
return myBloc;
}
return schuldenBloc;
},
);
}
The error Bad state: No element was happening because I'm trying to get the first
item of an empty iterable keys
inside the BlocProvider
.
var list = myIterable.keys.first; // keys was empty
Author by
djalmafreestyler
Updated on December 12, 2022Comments
-
djalmafreestyler over 1 year
I'm using bloc_pattern package and I have a bloc that will manage a favorite IconButton, I have a bool variable called _isFavorite that will pass to the stream showing if the item is favorite or not. When I instantiate the FavoriteBloc I get the error: "Bad state: No element". I seeded with a value "false" but it doesn't work. I followed the example of the package, does anyone know how I fix it?
class FavoritesBloc extends BlocBase { FavoritesBloc(); bool _isFavorite; var _favoriteController = BehaviorSubject<bool>.seeded(false); Stream<bool> get outFavorite => _favoriteController.stream; Sink<bool> get inFavorite => _favoriteController.sink; @override void dispose() { _favoriteController.close(); super.dispose(); } }
Main
void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( blocs: [ Bloc((i)=> FavoritesBloc()) , ], child: MaterialApp( debugShowCheckedModeBanner: false, home: HomeScreen(), theme: ThemeData( primaryColor: Colors.cyan[700], ), ), ); } }
The class where I call the FavoriteBloc:
class DetailScreen extends StatelessWidget { final favoritesBloc = BlocProvider.getBloc<FavoritesBloc>(); @override Widget build(BuildContext context) { return WillPopScope( onWillPop: (){ Navigator.of(context).pop(); }, child: Material( child: OKToast( child: SafeArea( child: Scaffold( body: Stack( children: <Widget>[ StreamBuilder( stream: favoritesBloc.outFavorite, builder: (context, snapshot){ bool isFavorite = snapshot.data; return Align( alignment: Alignment.topRight, heightFactor: 200, child: IconButton( icon: Icon( FontAwesomeIcons.solidHeart, color: isFavorite == true ? Colors.redAccent : Colors.white, size: 35, ), onPressed: (){ } ), ); }, ), }
-
djalmafreestyler almost 5 yearsThe error happens when I instantiate the Bloc class:" final favoritesBloc = BlocProvider.getBloc <FavoritesBloc> ();". Actually I already commented the StreamBuilder to test.
-
djalmafreestyler almost 5 yearsThank you. My import 'package:?/blocs//favorites_bloc.dart'; was two // before favorite_loc, looks like Android Studio does not warn of import errors in sub folders.
-
djalmafreestyler almost 5 yearsI already solved the problem, I accepted the Jacob Moura's answer. Thanks anyway.
-
Hoppeduppeanut almost 5 years@djalmafreestyler Even though you may have already solved the problem yourself, other users can still come along and offer other solutions which may help future users with the same issue solve their problems too. Who knows, these future solutions might work better than the solution you've already accepted 5 years down the line!