1

我正在使用带有 BloC 模式的颤振开发一个简单的待办事项应用程序。

它有一个显示 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.
    });
  }

}

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

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

4

0 回答 0