Flutter bloc emitted data not coming to the UI

I am making a flutter app and I want to show adds in the app. This is my first time working with admob. I created a test ad and it worked. I try to implement bloc because this app will eventually get big. But when I try to do that, it is not working.

Here is my home screen ui. Commented code here is the working one without bloc.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'bloc/ad_bloc.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final AdSize adSize = AdSize.banner;
  BannerAd? _bannerAd;

  final String adUnitId = Platform.isAndroid
      // Use this ad unit on Android...
      ? 'ca-app-pub-3940256099942544/6300978111'
      // ... or this one on iOS.
      : 'ca-app-pub-3940256099942544/2934735716';

  @override
  void initState() {
    // _loadAd();
    context
        .read<AdBloc>()
        .add(AdInit(adSize: AdSize.banner, adUnitId: adUnitId));
    super.initState();
  }

  @override
  void dispose() {
    _bannerAd!.dispose();
    super.dispose();
  }

  // Loads a banner ad.
  // void _loadAd() {
  //   final bannerAd = BannerAd(
  //     size: adSize,
  //     adUnitId: adUnitId,
  //     request: const AdRequest(),
  //     listener: BannerAdListener(
  //       // Called when an ad is successfully received.
  //       onAdLoaded: (ad) {
  //         if (!mounted) {
  //           ad.dispose();
  //           return;
  //         }
  //         setState(() {
  //           _bannerAd = ad as BannerAd;
  //         });
  //       },
  //       // Called when an ad request failed.
  //       onAdFailedToLoad: (ad, error) {
  //         debugPrint('BannerAd failed to load: $error');
  //         ad.dispose();
  //       },
  //     ),
  //   );

  //   // Start loading.
  //   bannerAd.load();
  // }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: const Color(0xFFD9D9D9),
        appBar: AppBar(
          backgroundColor: const Color(0xFFF24405),
          centerTitle: true,
          title: const Text('Topup Card Scanner'),
        ),
        body: BlocConsumer<AdBloc, AdState>(
          listener: (context, state) {
            if (state is AdLoadSuccess) {
              _bannerAd = state.bannerAd;
              print('adloaded:::::::::::::: $_bannerAd');
            }

            if (state is AdLoadingError) {
              print('ERROR:::::::::::::::: ${state.message}');
            }
          },
          builder: (context, state) {
            return Column(
              children: [
                const Expanded(
                  child: Center(
                    child: Text('Home Screen'),
                  ),
                ),
                state is AdLoadSuccess
                    ? Expanded(
                        child: Align(
                          alignment: Alignment.bottomCenter,
                          child: AdWidget(ad: _bannerAd!),
                        ),
                      )
                    : Container(),
              ],
            );
          },
        ),
      ),
    );
  }
}

And this is the bloc I created

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:meta/meta.dart';

part 'ad_event.dart';
part 'ad_state.dart';

class AdBloc extends Bloc<AdEvent, AdState> {
  AdBloc() : super(AdInitial()) {
    on<AdInit>(initiateAdmob);
  }

  initiateAdmob(AdInit event, Emitter<AdState> emit) async {
    try {
      emit(AdLoading());
      final bannerAd = BannerAd(
        size: event.adSize,
        adUnitId: event.adUnitId,
        request: const AdRequest(),
        listener: BannerAdListener(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
            // if (!mounted) {
            //   ad.dispose();
            //   return;
            // }
            // setState(() {
            // BannerAd? bAd;
            // bAd = ad as BannerAd;
            // if (ad == null) {
            //   ad.dispose();
            // }
            // else {
            //   print('BannerADD:::::::::::::::::::::: $bAd');
            //   emit(AdLoadSuccess(bannerAd: bAd!));
            // }

            // });
            emit(AdLoadSuccess(bannerAd: ad as BannerAd));
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (ad, error) {
            debugPrint('BannerAd failed to load: $error');
            ad.dispose();
            emit(AdLoadingError(message: error.toString()));
          },
        ),
      );

      // Start loading.
      bannerAd.load();
      // print('ad::::::::: $bAd');

      // if (bAd != null) emit(AdLoadSuccess(bannerAd: bAd!));
    } catch (e) {
      emit(AdLoadingError(message: e.toString()));
    }
  }
}

sorry if this is a messy code. I left the comments to your reference.

I don’t know this is doable with bloc I just tried anyway.
Any info will help me to solve this issue.

Thankyou. 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật