我正在使用Flutter开发一个简单的待办事项应用程序。
该应用程序具有多个视图和表(SQLite)。
对于Todo ui,它可以正常工作,但Tag ui不起作用。
第一个代码是Todo ui。 (效果很好)。
class TodosPage extends StatelessWidget {
final TodoRepository _todoRepository;
final TodosBloc bloc;
TodosPage(this._todoRepository) : this.bloc = TodosBloc(_todoRepository);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: StreamBuilder<List<Todo>>(
stream: bloc.todos,
builder: (context, snapshot) {
if (snapshot.data == null) {
return Text("Empty");
}
return ListView(
children: snapshot.data.map(_buildItem).toList(),
);
}),
),
);
}
Widget _buildItem(Todo todo) => Text(todo.name);
这是Tag ui(它不起作用)。
class TagsPage extends StatefulWidget {
final TagRepository _tagRepository;
TagsPage(this._tagRepository);
@override
State<StatefulWidget> createState() => _TagsState(_tagRepository);
}
class _TagsState extends State<TagsPage> {
final TagRepository _tagRepository;
final TagsBloc bloc;
_TagsState(this._tagRepository) : this.bloc = TagsBloc(_tagRepository);
@override
Widget build(BuildContext context) => Scaffold(
body: Center(
child: StreamBuilder<List<Tag>>(
stream: bloc.tags,
builder: (context, snapshot) {
if (snapshot.data == null || snapshot.data.isEmpty) {
print("[TagsPage][JOS] build - snapshot.data is null");
} else {
return ListView(
children: snapshot.data.map(_buildTagItem).toList(),
);
}
},
),
),
);
_buildTagItem(Tag tag) => Text(tag.name);
}
TagBloc在这里:
class TagsBloc {
final TagRepository _tagRepository;
final _getTagsSubject = PublishSubject<List<Tag>>();
Stream<List<Tag>> get tags => _getTagsSubject.stream;
TagsBloc(this._tagRepository) {
_getTags();
}
Future<Null> _getTags() async {
await _tagRepository.getAll().then((list) {
_getTagsSubject.add(list);
});
}
}
运行时,Tag ui显示以下错误日志:
04-07 00:13:45.698 25181-25195/com.youknow.jos I/flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
04-07 00:13:45.708 25181-25195/com.youknow.jos I/flutter: The following assertion was thrown building StreamBuilder<List<Tag>>(dirty, dependencies:
04-07 00:13:45.708 25181-25195/com.youknow.jos I/flutter: [_LocalizationsScope-[GlobalKey#49b9d], _InheritedTheme], state: _StreamBuilderBaseState<List<Tag>,
04-07 00:13:45.708 25181-25195/com.youknow.jos I/flutter: AsyncSnapshot<List<Tag>>>#65ac3):
04-07 00:13:45.708 25181-25195/com.youknow.jos I/flutter: type 'List<dynamic>' is not a subtype of type 'List<Widget>'
04-07 00:13:45.715 25181-25195/com.youknow.jos I/flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
04-07 00:13:45.715 25181-25195/com.youknow.jos I/flutter: more information in this error message to help you determine and fix the underlying cause.
04-07 00:13:45.715 25181-25195/com.youknow.jos I/flutter: In either case, please report this assertion by filing a bug on GitHub:
04-07 00:13:45.715 25181-25195/com.youknow.jos I/flutter: https://github.com/flutter/flutter/issues/new?template=BUG.md
04-07 00:13:45.715 25181-25195/com.youknow.jos I/flutter: When the exception was thrown, this was the stack:
04-07 00:13:45.724 25181-25195/com.youknow.jos I/flutter: #0 _TagsState.build.<anonymous closure> (package:jos/pages/tags.dart:49:62)
Todo和Tag没有区别。 我认为唯一的区别是StatefulWidget / StatelessWidget。 我不知道该如何解决这个问题。
答案 0 :(得分:1)
您尚未在_buildTagItem
上声明返回类型。更改此:
_buildTagItem(Tag tag) => Text(tag.name);
对此:
Widget _buildTagItem(Tag tag) => Text(tag.name);
结果,您的map(...).toList()
返回List<dynamic>
而不是List<Widget>
-因此出现错误。