扑扑状态管理不会在调用事件时更改ui

时间:2020-03-06 12:47:53

标签: flutter dart bloc flutter-bloc

我是扑朔迷离的新人。 我的问题是,当我更改应用程序的状态时,UI无法更新。 ui只会在我使用热重载时更改。

其状态代码为

import 'package:flutter_audio_query/flutter_audio_query.dart';
class MusicState {
 List<SongInfo> songs;
String songDisplayName = "";
MusicState._();
  String get props => songDisplayName;
  factory MusicState.initial() {
    return MusicState._()..songs = new List<SongInfo>();
  }
}

这是我的活动代码

abstract class MusicEvent{}
class GetMusic extends MusicEvent{
  GetMusic();
}
class ChangeMusic extends MusicEvent{
  final String songDisplayName;
  ChangeMusic(this.songDisplayName);
}

这也是我的集团代码:

import 'package:bloc/bloc.dart';
import 'package:music_player/bloc/music_event.dart';
import 'package:music_player/bloc/music_state.dart';
import 'package:flutter_audio_query/flutter_audio_query.dart';
class MusicBloc extends Bloc<MusicEvent, MusicState> {
  @override
  MusicState get initialState => MusicState.initial();
  @override
  Stream<MusicState> mapEventToState(MusicEvent event) async* {
    if (event is GetMusic) {
      FlutterAudioQuery audioQuery = new FlutterAudioQuery();
      state.songs = await audioQuery.getSongs();
      yield state;
    }
    if (event is ChangeMusic) {
      state.songDisplayName = event.songDisplayName;
      yield state;
    }
  }
}

它是我想更改但无法更改的小部件:

  Widget build(BuildContext context) {
    return BlocBuilder<MusicBloc,MusicState>(
      builder: (context, state) {
        return  ListTile(
          leading: CircleAvatar(
            child: Text("state.songDisplayName[0]"),
          ),
          title: Text(state.songDisplayName),
          subtitle: Text(state.songDisplayName),
          trailing: Icon(Icons.play_arrow),
        );
      },
    );
  }

我也检查了事件,并确定事件和集团能够正常工作。

请帮助我

谢谢。

0 个答案:

没有答案