作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 bloc 模式创建带有 API 数据的 ListView ,错误如下:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 5120 pos 12: 'child == _child': is not true.
import 'package:Instant_Feedback/Dashboard/PeopleList/bloc/bloc.dart';
import 'package:Instant_Feedback/People/strongConnection_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class PeopleListing extends StatefulWidget {
@override
State<StatefulWidget> createState() => _PeopleListingState();
}
class _PeopleListingState extends State<PeopleListing> {
PeopleListBloc peopleBloc;
@override
void initState() {
super.initState();
peopleBloc = BlocProvider.of<PeopleListBloc>(context);
peopleBloc.dispatch(DisplayPeopleList());
}
@override
Widget build(BuildContext context) {
return BlocBuilder(
bloc: peopleBloc,
builder: (context, state){
if (state is PeopleUninitializedState) {
print("PeopleUninitializedState");
} else if (state is PeopleFetchingState) {
print("PeopleFetchingState");
} else if (state is PeopleFetchingState) {
print("PeopleFetchingState");
} else {
final stateAsPeopleFetchedState = state as PeopleFetchedState;
final players = stateAsPeopleFetchedState.people;
return buildPeopleList(players);
}
},
);
}
Widget buildPeopleList(List<StrongConnection_model> people) {
print(people.length);
return Container(
child: Text('sdf sdkfh kdj'),
);
}
}
最佳答案
问题是,builder()
需要一个小部件,而您不会返回 有效小部件 在 if/else if
状况。尝试将您的代码更改为以下版本。
@override
Widget build(BuildContext context) {
return BlocBuilder(
bloc: peopleBloc,
builder: (context, state){
if (state is PeopleUninitializedState) {
<!-- Expects A Widget -->
print("PeopleUninitializedState");
return SizedBox();
} else if (state is PeopleFetchingState) {
<!-- Expects A Widget -->
print("PeopleFetchingState");
return SizedBox();
} else if (state is PeopleFetchingState) {
<!-- Expects A Widget -->
print("PeopleFetchingState");
return SizedBox();
} else {
final stateAsPeopleFetchedState = state as PeopleFetchedState;
final players = stateAsPeopleFetchedState.people;
return buildPeopleList(players);
}
},
);
}
关于flutter - 失败的断言行 5120 pos 12 : 'child = _child' is not true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58213530/
我是一名优秀的程序员,十分优秀!