0

Why my function in initState - the loop doesnt work? I mean that I just wanna run function

 _newsBloc.add(GetCompanyList());

Only in first time app run when list of users is empty. I just wanna make it because when I'm changing activities to other I have to wait again to fetch data from my api, but i dont want it. If it ask Api 1 time I do not need it anymore. Then I'm checkig length of list in init state it always return 0. Any ideas?

This is my code:

           class _CompanyActivityState extends State<CompanyActivity> with SingleTickerProviderStateMixin {
              List<Company> _users = [];
              final CompanyBloc _newsBloc = CompanyBloc();
            
              @override
              void initState() {
                super.initState();
if(_users.length < 0 ) {
                _newsBloc.add(GetCompanyList());
}

              }
        
    @override
      Widget build(BuildContext context) {
        return Scaffold(
            drawer: NavigationDrawerWidget(),
            appBar: buildAppBar(),
            floatingActionButton: Stack(
              children: <Widget>[
                Stack(
          body: createListViewWidgets());
      }
    
          Widget createListViewWidgets() {
            return Container(
              margin: EdgeInsets.all(8.0),
              child: BlocProvider(
                create: (_) => _newsBloc,
                child: BlocListener<CompanyBloc, CompanyState>(
                  listener: (context, state) {
                    if (state is CompanyError) {
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                        content: Text(state.message),
                      ));
                    }
        
                  },
                  child: BlocBuilder<CompanyBloc, CompanyState>(
                    builder: (context, state) {
                      if (state is CompanyInitial) {
                        return _buildLoading();
                      } else if (state is CompanyLoading) {
                        return _buildLoading();
                      } else if (state is CompanyLoaded) {
                        _users = state.company;
                        return _listViewCard(context, state.company);
                      } else if (state is CompanyError) {
                        return Container();
                      } else {
                        return Container();
                      }
                    },
                  ),
                ),
              ),
            );
          }
        }

Bloc which works;

class CompanyBloc extends Bloc<CompanyEvent, CompanyState> {

  List<Company> mList = [];
  final ApiRepository _apiRepository = ApiRepository();

  CompanyBloc() : super(CompanyInitial()) {
    on<GetCompanyList>((event, emit) async {
      try {
        emit(CompanyLoading());
        mList.addAll(await _apiRepository.fetchCompanyList());
        emit(CompanyLoaded(mList));
        if (mList.length == 0) {
          emit(CompanyError('List is empty'));
        }
      } on NetworkError {
        emit(CompanyError("Failed to fetch data. is your device online?"));
      }
    });
  }
}

And state which is used to create _users list:

class CompanyLoaded extends CompanyState {
  final List<Company> company;
  const CompanyLoaded(this.company);

  @override
  List<Object> get props => throw UnimplementedError();

}
vowoker
  • 13
  • 4

0 Answers0