Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

bloc

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

like image 256
Panks Avatar asked Oct 03 '19 06:10

Panks


2 Answers

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);
            }
        },
    );
}
like image 177
Aziz Kaukawala Avatar answered Oct 04 '22 01:10

Aziz Kaukawala


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.

like image 25
gsm Avatar answered Oct 04 '22 03:10

gsm