Event added to Bloc and yield state but it not recall bloc builder

when I add an event from a stateless widget by using BlocProvider.of<>, it really adds event and yield state, and BlocBuilder work and change UI,

But, when adding an event from a separate class, it really adds an event to the bloc and onTransition work, but not yield a new state, and BlocBuilder not work to change UI.

the main :

main(){
return runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
 MQTTManager x = MQTTManager();
 return MaterialApp(
   home: BlocProvider(
     lazy: false,
     create:(context)=>MqttblocBloc(MQTTManager())..add(StartConnect()) ,
     child:Home() ,
   ),
 );
}
}

the bloc :

class MqttblocBloc extends Bloc<MqttblocEvent, MqttblocState> {
  MQTTManager manager = MQTTManager() ;
  MqttblocBloc(this.manager) : super(MqttblocInitial());

 @override
 Stream<MqttblocState> mapEventToState(
   MqttblocEvent event,
 ) async* {
   if(event is StartConnect){
    try{
      manager.initializeMQTTClient();
      manager.connect();
      yield MqttblocInitial();
    }catch(e){
      print(e);
    }
   }

   else if(event is ConnectedEvent) {
     try{
       print('inBloc connect....');
       yield ConnectedState(MQTTManager.s);
     }catch(e){
       print(e);
     }
   }


   else if(event is PublishedEvent){
     try{
       manager.publishsw1('on');
       print('inBloc publish........');
       yield PublishState(manager.getText1());
     }catch(e){
       print(e);
     }
   }

   else if(event is DisconnectedEvent) {
     try{
       print('inBloc And Disconnnnn....');
       yield DisconnectState(MQTTManager.u);
     }catch(e){
       print(e);
     }
   }

 }
 @override
 void onTransition(Transition<MqttblocEvent, MqttblocState> transition) {
   super.onTransition(transition);
   print(transition);
 }
}

and here separate class where I listen to server and add events to bloc :

Amar() {
    MqttblocBloc bloc = MqttblocBloc(this);
     client.subscribe("\$events", MqttQos.atLeastOnce);

    client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
      final MqttPublishMessage recMess = c[0].payload;
      final String pt =
      MqttPublishPayload.bytesToStringAsString(recMess.payload.message);

      var z =  DetectEvent.fromJson(json.decode(pt));
      if(z.type == 'connected'){
        var connected = Connected.fromJson(json.decode(pt));
        print (connected.type);
        s = connected.connection.name;
        setText1(connected.connection.name);
        bloc..add(ConnectedEvent());


      }
      else if(z.type == 'disconnected'){
        var disconnected = Disconnected.fromJson(json.decode(pt));
        print (disconnected.type) ;
        u = disconnected.disconnection.id;
        setText1(disconnected.disconnection.id);
        bloc..add(DisconnectedEvent());

      }
      else if(z.type == 'published'){
        var published = Published.fromJson(json.decode(pt));
        print(published.type) ;
        setText1(published.type);
      }

and that is a stateless widget and use blocbuider :

class Home extends StatelessWidget {
MQTTManager v = MQTTManager();
  @override
  Widget build(BuildContext context) {
    MqttblocBloc p = BlocProvider.of<MqttblocBloc>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Bloc MQTT'),
        actions: [
          Row(
            children: [
              IconButton(
                icon: Icon(Icons.wb_incandescent,
                ),
                onPressed: (){
                 p.add(PublishedEvent());
                },
              ),
            ],
          )
        ],
      ),
      body: Center(
        child: BlocBuilder<MqttblocBloc,MqttblocState>(
         // buildWhen: (previuosState , currentState)=>currentState.runtimeType !=previuosState.runtimeType,

          builder:(context , state){
            if(state is MqttblocInitial){
              return CircularProgressIndicator();
            }
            else if(state is ConnectedState){
              return IconButton(
                icon: Icon(Icons.wifi),
                onPressed: (){},
              );
            }
            else if(state is PublishState){
              return RaisedButton(
                child: Text('${state.x}'),
                onPressed: (){},
              );
            }
            else if(state is DisconnectState){
              return IconButton(
                icon: Icon(Icons.wb_incandescent),
                onPressed: (){
                },
              );
            }
            return CircularProgressIndicator();
          } ,
        ),
      )
    );
  }
}

bloc State :

@immutable
abstract class MqttblocState extends Equatable {
 const MqttblocState();
  @override
  List<Object> get props => [];

}

class MqttblocInitial extends MqttblocState {}

class ConnectedState extends MqttblocState{
  final String x ;
  ConnectedState(this.x);
  @override
  List<Object> get props => [x];
}
class PublishState extends MqttblocState{
  final String x ;
  PublishState(this.x);
  @override
  List<Object> get props => [x];
}

class DisconnectState extends MqttblocState{
  final String x ;
  DisconnectState(this.x);
  @override
  List<Object> get props => [x];
}

and bloc events

@immutable
abstract class MqttblocEvent extends Equatable  {
  MqttblocEvent();
  @override
  List<Object> get props => [];
}

class StartConnect extends MqttblocEvent{}

class ConnectedEvent extends MqttblocEvent{}

class PublishedEvent extends MqttblocEvent{}

class DisconnectedEvent extends MqttblocEvent{}




Read more here: https://stackoverflow.com/questions/64883541/event-added-to-bloc-and-yield-state-but-it-not-recall-bloc-builder

Content Attribution

This content was originally published by ahmed zahran at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: