断言行 5120 pos 12 失败:'child = _child' 不正确
Failed assertion line 5120 pos 12: 'child = _child' is not true
我正在尝试使用 bloc 模式创建包含 API 数据的列表视图,错误如下:
'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'),
);
}
}
错误:
简单的答案就是导入库**
- 导入'package:flutter/src/widgets/framework.dart';
这对我有用,希望对你也有用。
问题是,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);
}
},
);
}
将脚手架添加到您的 error-causing 小部件
对于其他人,那些寻找此异常但答案与您的代码不匹配的人我的提示是您应该跟踪您的 child 异常。当您的 child 类 有问题时,通常会发生此异常。尝试撤消更改。参见 child 类 initState 选项。
我正在尝试使用 bloc 模式创建包含 API 数据的列表视图,错误如下:
'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'),
);
}
}
错误:
简单的答案就是导入库**
- 导入'package:flutter/src/widgets/framework.dart';
这对我有用,希望对你也有用。
问题是,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);
}
},
);
}
将脚手架添加到您的 error-causing 小部件
对于其他人,那些寻找此异常但答案与您的代码不匹配的人我的提示是您应该跟踪您的 child 异常。当您的 child 类 有问题时,通常会发生此异常。尝试撤消更改。参见 child 类 initState 选项。