'A value of type 'Null' can't be assigned to a parameter of type 'Widget Function(BuildContext, CounterState)' in a const constructor

This is an project where I'm trying to implement (and understand) Bloc methodology.

I got the error:

A value of type 'Null' can't be assigned to a parameter of type 'Widget Function(BuildContext, CounterState)' in a const constructor.

on the following code (this is the main.dart):

import 'package:example20221002/cubit/counter_cubit.dart';
import 'package:example20221002/cubit/counter_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => CounterCubit(),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const BlocBuilder<CounterCubit, CounterState>(
              builder: (context, state) {  // --< ERROR start here
                 return Text(
                  'Counter Value:'+ state.counterValue.toString(),
                );
              },
            ),
           
            Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
              FloatingActionButton(
                onPressed: () {
                  BlocProvider.of<CounterCubit>(context).dec();
                },
                tooltip: 'Decrement',
                child: const Icon(Icons.add),
              ),
              FloatingActionButton(
                onPressed: () {
                  BlocProvider.of<CounterCubit>(context).inc();
                },
                tooltip: 'Inc',
                child: const Icon(Icons.add_alarm),
              ),
            ])
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

           

the countercubit .dart is:

import 'package:bloc/bloc.dart';
import 'package:bloc/bloc.dart';
import 'counter_state.dart';

 class CounterCubit extends Cubit<CounterState>{
  CounterCubit() : super(CounterState(counterValue: 0));

  void inc() => emit (CounterState(counterValue: state.counterValue+1));
  void dec() => emit (CounterState(counterValue: state.counterValue-1));
 }
//import 'package:meta/meta.dart';
import 'counter_state.dart';
//part 'counter_state.dart';
 class CounterCubit extends Cubit<CounterState>{
  CounterCubit() : super(CounterState(counterValue: 0));
  void inc() => emit (CounterState(counterValue: state.counterValue+1));
  void dec() => emit (CounterState(counterValue: state.counterValue-1));
 }

and the counterstate.dart:

import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';
//import 'counter_cubit.dart';

//part of 'counter_cubit.dart';


class CounterState {
    int counterValue=0;
    
    CounterState ({
   required this.counterValue,
   });

}

As much as I tried, I don't understand the error:

  1. Why Null?
  2. On which parameter?
  3. How can I fix it?
  4. is the null is about BuildContext? CounterState?

Many thanks



Solution 1:[1]

Just remove the const keyword :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saad Lembarki