Home > Net >  Flutter_bloc package Giving weird errors When i try calling bloc provider
Flutter_bloc package Giving weird errors When i try calling bloc provider

Time:08-29

After install flutter_bloc 8.1.0 package i just shows this error when i start adding this code ->


  @override
  Widget build(BuildContext context) {
    return BlocProvider<CounterBloc>(
      create: (context) => CounterBloc(),
      child: MaterialApp(
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Container(),
      ),
    );
  }
}

i can't find the problem i thought a the beginning it's from build ios app but i started new project and start every thing form beginning and still get the same error.

Launching lib/main.dart on macOS in debug mode...
lib/main.dart:1
: Error: A class declaration must have a body, even if it is empty.
../…/src/cubit.dart:21
Try adding an empty body.
abstract class Cubit<State> extends Bloc Base<State> {
                                    ^^^^
: Error: A function declaration needs an explicit list of parameters.
../…/src/cubit.dart:21
Try adding a parameter list to the function declaration.
abstract class Cubit<State> extends Bloc Base<State> {
                                         ^^^^
: Error: Expected ',' before this.
../…/src/cubit.dart:23
  Cubit(State initialState) : super(initialState);
              ^^^^^^^^^^^^
: Error: Undefined name 'initialState'.
../…/src/cubit.dart:23
  Cubit(State initialState) : super(initialState);
              ^^^^^^^^^^^^
: Error: The class 'Cubit' is abstract and can't be instantiated.
../…/src/cubit.dart:23
  Cubit(State initialState) : super(initialState);
  ^^^^^
: Error: Expected ';' after this.
../…/src/cubit.dart:23
  Cubit(State initialState) : super(initialState);

                          ^
: Error: Expected an identifier, but got ':'.
../…/src/cubit.dart:23

Try inserting an identifier before ':'.
  Cubit(State initialState) : super(initialState);
                            ^
: Error: Unexpected token ';'.
../…/src/cubit.dart:23
  Cubit(State initialState) : super(initialState);

Bloc

import 'package:equatable/equatable.dart';
class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterBloc() : super(CounterInitial(counterValue: 0)) {on<CounterEvent>((state, emit) {});}
}
abstract class CounterEvent extends Equatable {}
abstract class CounterState extends Equatable {
  const CounterState();
  @override
  List<Object> get props => [];}
class CounterInitial extends CounterState {
  int counterValue;
  CounterInitial({required this.counterValue});
  @override
  List<Object> get props => [counterValue];
}

thanks for help.

CodePudding user response:

The issue is you are extending Equatable instead of state class on CounterInitial.

It will be

class CounterInitial extends CounterState {
class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterBloc() : super(const CounterInitial(counterValue: 0)) {
    on<CounterEvent>((state, emit) {});
  }
}

abstract class CounterEvent extends Equatable {}

abstract class CounterState extends Equatable {
  const CounterState();
  @override
  List<Object> get props => [];
}

class CounterInitial extends CounterState {
  final int counterValue;
  const CounterInitial({required this.counterValue});
  @override
  List<Object> get props => [counterValue];
}

Full snippet

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

class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterBloc() : super(const CounterInitial(counterValue: 0)) {
    on<CounterEvent>((state, emit) {});
  }
}

abstract class CounterEvent extends Equatable {}

abstract class CounterState extends Equatable {
  const CounterState();
  @override
  List<Object> get props => [];
}

class CounterInitial extends CounterState {
  final int counterValue;
  const CounterInitial({required this.counterValue});
  @override
  List<Object> get props => [counterValue];
}

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider<CounterBloc>(
      create: (context) => CounterBloc(),
      child: MaterialApp(
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(),
      ),
    );
  }
}

CodePudding user response:

I think the problem was in something got wrong when i wanted to install the package Flutter_bloc so i delete all the packages form flutter sdk and it works again.

so the solution is just ''' rm -f flutter/.pub-cache/hosted/pub.dartlang.org
''' and Thanks @yasin_Sheikh

  • Related