最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dart - [SOLVED]Flutter bloc doesnt listen to state change - Stack Overflow

programmeradmin1浏览0评论

[SOLVED: i was using BlocProvider(); instead of BlocProvider.value(); so i created multiple instances of bloc]

I am changing the state in my profile bloc in response to a onTap. Then i want my bloclistener to listen to the state change and use my navigation to go to a new screen. I've already done this in another section of my app but now that i try the state change is made but the bloc doesnt listen

The relevant bloc code

FutureOr<void> fetchCharacterFamily(
      FetchCharacterFamily event, Emitter<ProfileState> emit) async {
        print("adadf");

    emit(FamilyFetchingLoadingState());
print("adadf1");

    String cuid = event.cuid;
print("adadf2");

    List<Relations> familyTree = await CharacterRepository.getcharacterFamilyTree(cuid);

print("adadf3");

    emit(FamilyFetchingSuccessfulState(
        familyTree: familyTree, cuid: "$cuid"));
  }
}

State classes

class FamilyFetchingSuccessfulState extends ProfileState {
  final List<Relations> familyTree;
  final String cuid;


  const FamilyFetchingSuccessfulState({required this.familyTree, required this.cuid});
}
class FamilyFetchingLoadingState extends ProfileState {}

Event class

class FetchCharacterFamily extends ProfileEvent {
  final String cuid;
    const FetchCharacterFamily(this.cuid);
}

The bloc consumer in my profile page

return BlocConsumer<ProfileBloc, ProfileState>(
      listener: (context, state) {
      if (state is CharacterExists) {
        BlocProvider.of<ProfileBloc>(context).add(FetchCharacter());
      }
      if (state is FamilyFetchingLoadingState) {
        GoRouter.of(context).go('/profile/family');
      }
    }, 
    
    builder: (context, state) {...}

The builder in my profile page

 Builder(builder: (context) {
                        print("we are here");
                        if (successState.characters.isNotEmpty) {
                          print("inside if statement");
                          return Padding(
                            padding: const EdgeInsets.only(top: 80),
                            child: Center(
                              child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: CharacterRegularCard(
                                    cuid: successState.characters[0].cuid,
                                    name: successState.characters[0].name,
                                    balance: successState.characters[0].balance,
                                    alive: successState.characters[0].alive,
                                    age: successState.characters[0].age,
                                    sex: successState.characters[0].sex,
                                  )),
                            ),
                          );
                        } else {
                          print("inside else ");
                          return Padding(
                            padding: const EdgeInsets.only(top: 80),
                            child: Container(
                              height: 450,
                              color: Colors.yellow,
                            ),
                          );
                        }
                      })

The onPressed inside my CharacterRegularCard widget


                  onPressed: () async {
                    BlocProvider.of<ProfileBloc>(context).add(
                        FetchCharacterFamily(
                            "c8290be3-394c-4bd6-b4cb-642ad6d49656"));
                  },

What am i doing wrong?:(

发布评论

评论列表(0)

  1. 暂无评论