断言行5120 pos 12失败:'child = _child'不正确

时间:2019-10-03 06:36:42

标签: flutter bloc

我正在尝试使用bloc patter从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'),
    );
  }
}

错误: enter image description here

4 个答案:

答案 0 :(得分:2)

问题是,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);
            }
        },
    );
}

答案 1 :(得分:0)

对于其他人,那些寻找此异常但答案与您的代码不匹配的人,我的提示是您应该跟踪您的子异常。当您的子类出现问题时,通常会发生此异常。尝试撤消更改。请参阅子类 initState 以获取选项。

答案 2 :(得分:-1)

将脚手架添加到导致错误的小部件中

答案 3 :(得分:-2)

简单的答案就是导入库**

  • 导入“ package:flutter / src / widgets / framework.dart”;

这对我有用,我希望它对您也有用。