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());
}