Bloc无法在对话框中返回数据

时间:2019-04-07 08:47:11

标签: dart flutter bloc

我正在使用带有BloC模式的颤动开发一个简单的todo应用程序。

它具有用于显示TodoDetails的ui。

用户单击按钮时,将显示一个新的SimpleDialog。

我想在SimpleDialog中显示一些标签列表,例如:

class AddEditTodoPage extends StatefulWidget {
  final TodoRepository todoRepository;
  final TagRepository tagRepository;
  final Todo todo;
  final SaveTodoBloc bloc;

  AddEditTodoPage({this.todoRepository, this.tagRepository, this.todo})
      : bloc = SaveTodoBloc(
            todoRepository: todoRepository,
            tagRepository: tagRepository,
            todo: todo);

  @override
  State<StatefulWidget> createState() => _AddEditTodoPageState(todo: todo);
}

class _AddEditTodoPageState extends State<AddEditTodoPage> {
  final Todo todo;
  _AddEditTodoPageState({this.todo});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: StreamBuilder<Tag>(
               stream: widget.bloc.tag,
               builder: (context, snapshot) {
                 final tag = snapshot.data;
                 return OutlineButton(
                     onPressed: () async {
                         final selectedTag = await showDialog<TagSelection>(
                           context: context,
                           builder: (context) => _showTagSelectDialog(context),
                         );
                     },
                 );
               }},
    );
  }

  _showTagSelectDialog(BuildContext context) => SimpleDialog(
        title: Text("Select a Tag or create a new one"),
        children: <Widget>[
          StreamBuilder<List<Tag>>(
              stream: widget.bloc.tags,
              builder: (context, snapshot) {
                final tagList = snapshot.data;
                if (tagList == null || tagList.isEmpty) {
                  // This is always 'null'!!!
                  return SizedBox();
                } else {
                  return ListView(
                    children: tagList.map(_buildTagName).toList(),
                  );
                }
              }),
        ],
      );

  Widget _buildTagName(Tag tag) => Text(tag.name);
}

所以我的集团正在获取TagList,如:

class SaveTodoBloc {

  final TodoRepository todoRepository;
  final TagRepository tagRepository;
  final Todo todo;

  SaveTodoBloc({this.todoRepository, this.tagRepository, this.todo}) {
    if (tagRepository != null) {
      _getTags();
    }
  }

  final _getTagsSubject = PublishSubject<List<Tag>>();

  Stream<List<Tag>> get tags => _getTagsSubject.stream;

  Future<Null> _getTags() async {
    await tagRepository.getAll().then((list) {
      _getTagsSubject.add(list);
      print("[SaveTodoBloc][JOS] _getTags - $list"); // It resturns correct list of Tags.
    });
  }

}

当我检查日志时,块逻辑返回正确的标签列表。

但是当我显示对话框时,它没有标签列表。 该列表为空。

0 个答案:

没有答案