in Education by
I am trying to handle 3 different states in my app, in the initState of my home, I call the event "OnInitial" pretending to be a method that fetches data from the local DB. As you can see in the method on I am setting "6" to the variable "number" as I said, pretending that this would be the data that I brought from the DB. My event finishes fetching the data and emits the state "ProductsTercero" already with "the data assigned". In my home, if the state is ProductsTercero I paint a column with the "msg" and the "number". When inital finishes my screen is redrawn correctly and shows But my problem is the following. When I press the button called "Tercero" that triggers the event "OnTercero" In my Bloc, when this event is called, it sets a 'New msg' to the variable "msg" of ProducsTercero. What does this? the next: It removes the "number 6" that I had set before, that is, regarding the simulation, I would be deleting all the data that I brought from the DB. my expected result was: New msg 6 but i got New msg 0 I debug and it seems like it's always creating a new ProductsThird object. even if I do ProductsTercero().copyWith(msg: 'New msg'); is the same. I have worked with only 1 state and this does not happen to me, because I always do a state.copyWith(); and the other values of that state are always maintained no matter if i pass 1 attribute of 5 attributes it has. but here, having more than one state and having an abstract state class, I can't write emit(state.copyWith(msg: 'New msg'); What am I doing wrong? What would be the correct way to emit on X state and that this does not happen? Event, state and bloc code: part of 'products_bloc.dart'; @immutable abstract class ProductsEvent {} class OnInitial extends ProductsEvent {} class OnSegundo extends ProductsEvent {} class OnTercero extends ProductsEvent {} //**************************************************************// part of 'products_bloc.dart'; @immutable abstract class ProductsState {} class ProductsInitial extends ProductsState { final String? msg; final int? numero; ProductsInitial({numero, msg}) : msg = msg ?? 'MSG: Default Inicial', numero = numero ?? 0; ProductsInitial copywith({String? msg, int? numero}) => ProductsInitial(msg: msg ?? this.msg, numero: numero ?? this.numero); } class ProductsSegundo extends ProductsState { final String? msg; final int? numero; ProductsSegundo({numero, msg}) : msg = msg ?? 'Second: Default Msg', numero = numero ?? 0; ProductsSegundo copywith({String? msg, int? numero}) => ProductsSegundo(msg: msg ?? this.msg, numero: numero ?? this.numero); } class ProductsTercero extends ProductsState { final String? msg; final int? numero; ProductsTercero({numero, msg}) : msg = msg ?? 'Third: Default Msg', numero = numero ?? 0; ProductsTercero copywith({String? msg, int? numero}) => ProductsTercero(msg: msg ?? this.msg, numero: numero ?? this.numero); } //**********************************************************// import 'package:bloc/bloc.dart'; import 'package:meta/meta.dart'; part 'products_event.dart'; part 'products_state.dart'; class ProductsBloc extends Bloc { ProductsBloc() : super(ProductsInitial()) { on((event, emit) { emit(ProductsInitial().copywith( msg: 'Initial msg' )); emit(ProductsTercero(numero: 6)); }); on((event, emit) { emit(ProductsSegundo()); }); on((event, emit) { emit(ProductsTercero(msg: 'New msg')); }); } } Homepage code: import 'package:bloc_test/src/bloc/products/products_bloc.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @override State createState() => _HomePageState(); } class _HomePageState extends State { @override void initState() { BlocProvider.of(context).add(OnInitial()); super.initState(); } @override Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { return Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ (state is ProductsInitial) ? Column( children: [ Center(child: Text(state.msg!)), Center(child: Text(state.numero!.toString())), ], ) : (state is ProductsSegundo) ? Column( children: [ Center(child: Text(state.msg!)), Center(child: Text(state.numero!.toString())), ], ) : (state is ProductsTercero) ? Column( children: [ Center(child: Text(state.msg!)), Center(child: Text(state.numero!.toString())), ], ) : Center(child: Text('Nada')), SizedBox( height: 40, width: double.infinity, child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ ElevatedButton( onPressed: () { BlocProvider.of(context).add(OnInitial()); }, child: Text('Initial')), ElevatedButton( onPressed: () { BlocProvider.of(context).add(OnSegundo()); }, child: Text('Segundo')), ElevatedButton( onPressed: () { BlocProvider.of(context).add(OnTercero()); }, child: Text('Tercero')) ], ), ), ], ), ); }, ); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are not passing num value on emit(ProductsTercero(msg: 'New msg')); Since num == null then it is set to 0, as defined by your code. Try Replacing like below on((event, emit) { emit(ProductsTercero(msg: 'New msg',numero: 6)); });

Related questions

0 votes
    I'm trying to re-play a Flare animation in Flutter. Not loop the animation when it has complete. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    What is a trade bloc? Please answer the above question....
asked Aug 4, 2022 in Education by JackTerrance
0 votes
    How does Maven manage dependencies?...
asked Feb 5, 2023 in Technology by JackTerrance
0 votes
    ____________ retains data about how processes combine and manage threads and allocations which they use. (1)Flow File (2)Connection (3)Flow Controller...
asked Apr 20, 2021 in Technology by JackTerrance
0 votes
    ____________ retains data about how processes combine and manage threads and allocations which they use. (1)Flow File (2)Connection (3)Flow Controller...
asked Apr 19, 2021 in Technology by JackTerrance
0 votes
    How do you manage risk in your personal life?...
asked Oct 10, 2020 in Credit by Editorial Staff
0 votes
    I'm working on a program that displays a list from an SQFlite table. The future for the snapshot ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    I'm using the official Google Maps Flutter plugin to show maps and it works perfectly well but now I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm new to Flutter Redux, I got a problem and I have no idea how to deal with it at all! ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I'm using flutter_map package and I need to draw path between 2 marker. I know map_view package provided ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I am trying to implement the reCaptcha function to my flutter app, but in the captcha registration I need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    "masses": [ { "Sunday": [ { "mass_timing": "7.00 AM", "mass_place": "First Mass", " ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    How to combine two Excel files with the same fields but different data (different years)?...
asked Oct 29, 2020 in Technology by JackTerrance
0 votes
    Which Ansible module is used to manage docker services and containers. (i)docker_login (ii)docker_container (iii)docker (iv)docker_service...
asked Jan 25, 2023 in Technology by JackTerrance
0 votes
    Which Ansible module is used to manage docker services and containers (i)docker_login (ii)docker_container (iii)docker (iv)docker_service...
asked Jan 24, 2023 in Technology by JackTerrance
...