Flutter Bloc await for response before continueing

Advertisements I have following code: class BidBloc extends Bloc<BidEvent, BidState> { final FirestoreRepository firestoreRepository; BidBloc({required this.firestoreRepository}) : super(BidsLoadingState()) { on<LoadAllBidsEvent>((event, emit) async { emit(BidsLoadingState()); Item item = event.item; Future getBids() async { List<Bid> bids = []; item.bids?.forEach((element) async { Bid? bid = await firestoreRepository.getBidByBidId(bidID: element); if (bid != null) { DbUser? dbUser = await firestoreRepository.getDBUserByDBUserId(… Read More Flutter Bloc await for response before continueing

Flutter: Context in FloatingActionButton cannot find the provider of <Bloc>

Advertisements It’s my first time using the BLoC pattern in Flutter. I have ran into a problem with providers and context. Here is my main.dart file. import ‘package:flutter/material.dart’; import ‘package:flutter_bloc/flutter_bloc.dart’; import ‘package:my_rescue/modules/screens/homepage/bloc/homepage_bloc.dart’; import ‘package:my_rescue/widgets/drawer.dart’; import ‘package:my_rescue/widgets/text_button.dart’; import ‘package:my_rescue/widgets/weather_forecast.dart’; import ‘package:my_rescue/widgets/app_bar.dart’; class HomePage extends StatefulWidget { const HomePage({super.key}); @override State<HomePage> createState() => _HomePageState(); } class _HomePageState… Read More Flutter: Context in FloatingActionButton cannot find the provider of <Bloc>

How to change state of cubit?

Advertisements image description here I want to change the state of cubit after 4 seconds when i click the button but it gives error : **BlocProvider.of() called with a context that does not contain a TestCubit. ======== Exception caught by gesture =============================================================== The following assertion was thrown while handling a gesture: BlocProvider.of() called with a… Read More How to change state of cubit?

A value of type 'Iterable<HospitalListModel>' can't be assigned to a variable of type 'List<HospitalListModel>'

Advertisements I got a flutter error A value of type ‘Iterable<HospitalListModel>’ can’t be assigned to a variable of type ‘List<HospitalListModel>’. This is my model: List<HospitalListModel> hospitalListModelFromJson(String str) => List<HospitalListModel>.from(json.decode(str).map((x) => HospitalListModel.fromJson(x))); String hospitalListModelToJson(List<HospitalListModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson()))); class HospitalListModel { HospitalListModel({ this.id, this.title, this.content, this.image, this.phone, this.coordinates, this.website, this.createdAt, this.updatedAt, }); dynamic id; dynamic… Read More A value of type 'Iterable<HospitalListModel>' can't be assigned to a variable of type 'List<HospitalListModel>'

How to provide context to BlocProvider.of without using BlocBuilder in flutter bloc

Advertisements This is my BlocProvider portion of code: late BuildContext _context; @override Widget build(BuildContext context) { final _formKey = GlobalKey<FormState>(); return BlocProvider<AccountBloc>( create: (context) { _context = context; return AccountBloc(); }, child: Scaffold( And inside the onPressed I use this.context: BlocProvider.of<AccountBloc>(this._context)..add(AddAccountEvent(account: account)); When I run it the error says: LateInitializationError: Field ‘_context@30149156’ has not been… Read More How to provide context to BlocProvider.of without using BlocBuilder in flutter bloc

Convert Default Flutter_bloc 8.0 Counter from Cubit to Bloc

Advertisements I’m trying to convert bloc’s simple cubit example to the bloc pattern but getting an …NoSuchMethodError: Class ‘_Emitter<MyObjectState>… error when I press the increment button. I believe my problem is the dynamic type I used for the emit parameter (changing it to MyObjectState results in an error that the expression isn’t a function). What… Read More Convert Default Flutter_bloc 8.0 Counter from Cubit to Bloc