1

In the process of learning bloc and have started to create a basic state and cubit but I am now getting the following error.

I should be seeing a grey screen in emulation mode instead, I see the following red error.

_TypeError (type 'Null' is not a subtype of type 'Color' of 'function result')

I'm not quite sure why, since the first state should be Color.grey?

Main.dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:bloctest/cubit/color_cubit.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocProvider(
        create: (context) => ColorCubit(),
        child: BlocBuilder<ColorCubit, ColorState>(builder: (context, state) {
          return Container(
              color: state.color,
              child: FloatingActionButton(
                onPressed: () {
                  print('we will change the state soon');
                },
              ));
        }),
      ),
    );
  }
}

color_state.dart

part of 'color_cubit.dart';

abstract class ColorState extends Equatable {
  final Color color;
  const ColorState(this.color);

  @override
  List<Object> get props => [];
}

class ColorInitial extends ColorState {
  const ColorInitial() : super(Colors.grey);
}

color_cubit.dart

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

part 'color_state.dart';

class ColorCubit extends Cubit<ColorState> {
  ColorCubit() : super(const ColorInitial());
}
VanCoon
  • 422
  • 4
  • 20
  • 6
    Your code seems to be fine, there is nothing wrong with what you just showed, maybe it's from somewhere else, if not just do `flutter clean` and run it again – Arin Faraj Jul 23 '22 at 18:00
  • 1
    That solved the issue. Hopefully Flutter becomes a bit more seamless so whatever happens in the background when flutter clean is run, is handled better in the future. – VanCoon Jul 23 '22 at 20:06

0 Answers0