Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a zero argument constructor in Dart flutter

Tags:

flutter

dart

bloc

What is a zero-argument constructor in Dart flutter?

while creating a bloc in flutter I am receiving the following error

  The superclass 'Bloc<QuoteEvent, QuoteState>' doesn't have a zero argument constructor.
Try declaring a zero argument constructor in 'Bloc<QuoteEvent, QuoteState>', or explicitly invoking a different constructor in 'Bloc<QuoteEvent, QuoteState>'.

kindly guide how to fix it. thanks

below is code

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

import 'package:random_quote/repositories/repositories.dart';
import 'package:random_quote/models/models.dart';
import 'package:random_quote/bloc/bloc.dart';

class QuoteBloc extends Bloc<QuoteEvent, QuoteState> {
  final QuoteRepository repository;

  QuoteBloc({@required this.repository}) : assert(repository != null);

  @override
  QuoteState get initialState => QuoteEmpty();

  @override
  Stream<QuoteState> mapEventToState(QuoteEvent event) async* {
    if (event is FetchQuote) {
      yield QuoteLoading();
      try {
        final Quote quote = await repository.fetchQuote();
        yield QuoteLoaded(quote: quote);
      } catch (_) {
        yield QuoteError();
      }
    }
  }
}
like image 989
Javeed Ishaq Avatar asked Jan 24 '23 15:01

Javeed Ishaq


1 Answers

initialState property was removed from flutter_bloc since v5.0.0. Here is migration guide.

You should use super() constructor instead:

class QuoteBloc extends Bloc<QuoteEvent, QuoteState> {
  final QuoteRepository repository;

  QuoteBloc({@required this.repository}) : 
    assert(this.repository != null),
    super(QuoteEmpty());

  ...
like image 141
Mol0ko Avatar answered Jan 31 '23 05:01

Mol0ko