Failed assertion line 5120 pos 12: 'child = _child' is not true
Asked Answered
R

6

15

I am trying to create a listview with API data using bloc pattern following is the error:

'package:flutter/src/widgets/framework.dart': Failed assertion: line 5120 pos 12: 'child == _child': is not true.

My list file:

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'),
    );
  }
}

Error: enter image description here

Ruthful answered 3/10, 2019 at 6:36 Comment(2)
Could you try to do null check to people?Davon
You are not returning any widget excep else case. You have to return a widget constantly inside builder. Otherwise you'll get this error. Just return a SizedBox outside of the if condition loop.Malvasia
L
6

Problem is, builder() expects a widget & you're not returning a valid widget in the if/else if conditions. Try changing your code to the below version.

@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);
            }
        },
    );
}
Lack answered 4/9, 2020 at 5:0 Comment(1)
but, i was also used return in each condition of futurebuilder. but, it still give a same error which i used chaining futurebuilderSpelldown
D
2

For Others, those who looking for this exception but the answer doesn't match with your code to my tip is you should trace your child exceptions. This exception usually happens when there is something wrong with your child classes. Try undoing changes. see child classes initState for option.

Dangerfield answered 20/7, 2021 at 6:6 Comment(0)
M
2

you need to close your app. not just hot restart it in your terminal because it will cause that error.

Marcellemarcellina answered 8/10, 2022 at 1:26 Comment(0)
A
0

In my case, it was the typo.

class OnboardingMultipleChoiceGrid extends StatefulWidget {
  final String title;
  final List<String> choices;
  final VoidCallback onTap;

  const OnboardingMultipleChoiceGrid({
    required this.title,
    required this.choices,
    required this.onTap,
    Key? key,
  }) : super(key: key);

  @override
  _OnboardingMultipleChoiceGridState createState() =>
      _OnboardingMultipleChoiceGridState();
}

class _OnboardingMultipleChoiceGridState
    extends State<OnboardingMultipleChoiceList> {
////

My state class was extending State<OnboardingMultipleChoiceList>, instead of State<OnboardingMultipleChoiceGrid>

Avenge answered 20/1 at 17:4 Comment(0)
G
-1

Addd Scaffold to your error-causing widget

Grasmere answered 16/10, 2020 at 10:13 Comment(0)
J
-8

The Simple Answer is just Import the library**

  • import 'package:flutter/src/widgets/framework.dart';

This works for me I hope it works for you as well.

Jinny answered 4/9, 2020 at 3:32 Comment(1)
This does't work for meJeremy

© 2022 - 2024 — McMap. All rights reserved.