断言行 5120 pos 12 失败:'child = _child' 不正确
Posted
技术标签:
【中文标题】断言行 5120 pos 12 失败:\'child = _child\' 不正确【英文标题】:Failed assertion line 5120 pos 12: 'child = _child' is not true断言行 5120 pos 12 失败:'child = _child' 不正确 【发布时间】:2020-02-01 10:36:51 【问题描述】:我正在尝试使用 bloc 模式创建一个包含 API 数据的列表视图,错误如下:
'package:flutter/src/widgets/framework.dart':断言失败:第 5120 行 pos 12:'child == _child':不正确。
我的列表文件:
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'),
);
错误:
【问题讨论】:
你能尝试对人们做空检查吗? 除了else
情况外,您不会返回任何小部件。您必须在builder
中不断返回一个小部件。否则你会得到这个错误。只需在 if 条件循环之外返回 SizedBox
。
【参考方案1】:
问题是,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);
,
);
【讨论】:
但是,我也被用在未来建造者的每个条件下。但是,它仍然给出与我使用链接 futurebuilder 相同的错误【参考方案2】:对于其他人,那些寻找此异常但答案与您的代码不匹配的人,我的提示是您应该跟踪您的子异常。当您的子班级出现问题时,通常会发生此异常。尝试撤消更改。请参阅子类 initState 的选项。
【讨论】:
【参考方案3】:将脚手架添加到导致错误的小部件
【讨论】:
【参考方案4】:简单的答案就是导入库**
导入 'package:flutter/src/widgets/framework.dart';这对我有用,我希望它也对你有用。
【讨论】:
以上是关于断言行 5120 pos 12 失败:'child = _child' 不正确的主要内容,如果未能解决你的问题,请参考以下文章
断言失败:第 551 行 pos 12:'child.hasSize':不正确
错误:未布置 RenderBox,断言失败:第 1940 行 pos 12:'hasSize'
断言失败:第 125 行第 12 行:'assertMidButtonStyles(navBarStyle, items!.length)'
断言失败:第 22 行 pos 14: 'url != null': is not true
Flutter 错误 - 断言失败:第 213 行 pos 15:'data != null':在从 firestore 获取数据时不正确
断言失败:第 24 行 pos 15:'color != null && color.alpha == 0xFF':不正确