Anson
Anson

Reputation: 1043

Flutter BLoc pass parameters

I'm trying to pass parameters to a bloc event following the bloc pattern, I have found this article however my dart document couldn't find the dispatch (event) method.

Flutter BLoC - How to pass parameter to event?

How do I pass parameters to something like this

main.dart

this works

_counterBloc.add(Counter.increment); 

But this doesn't

_counterBloc.add(Counter.increment(3));

bloc.dart

import 'package:bloc/bloc.dart';

enum CounterEvents { increment }

class CounterBloc extends Bloc<CounterEvents, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(CounterEvents event) async* {
    switch (event) {
      case CounterEvents.increment:
        print(event);
        yield state + 1;
        break;
    }
  }
}



Upvotes: 1

Views: 9786

Answers (3)

apteryxlabs
apteryxlabs

Reputation: 81

Consider making a custom event. Your solution should be something like this (haven't checked for bugs, but the paradigm is correct):

abstract class CounterEvent {}

class Increment extends CounterEvent {
    int amount;
    Increment({this.amount});
}

Then in your bloc:

class CounterBloc extends Bloc<CounterEvent, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(CounterEvent event) async* {
    if (event is Increment) {
       yield state + event.amount;
    }
  }
}

Upvotes: 0

Taleb
Taleb

Reputation: 2259

you should write CounterEvent like below:

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

enum EventStatus { INCREMENT, DECREMENT }

class CounterEvent {
  final int value;
  final EventStatus status;

  const CounterEvent({@required this.value, @required this.status});
}

class CounterBLoC extends Bloc<CounterEvent, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(event) async* {
    if (event.status == EventStatus.INCREMENT) {
      yield state + event.value;
    } else if (event.status == EventStatus.DECREMENT) {
      yield state - event.value;
    }
  }
}

and use them in the widget like below:

@override
  Widget build(BuildContext context) {
    final counterBloc = BlocProvider.of<CounterBLoC>(context);
    return Scaffold(
      body: Center(
        child: BlocBuilder<CounterBLoC, int>(
          builder: (ctx, state) {
            return Text(
              'count: $state',
              style: TextStyle(fontSize: 28),
            );
          },
        ),
      ),
      floatingActionButton: Align(
        alignment: Alignment.bottomRight,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            FloatingActionButton(
              onPressed: () {
                counterBloc
                    .add(CounterEvent(value: 5, status: EventStatus.INCREMENT));
              },
              child: Icon(Icons.add_circle),
            ),
            FloatingActionButton(
              onPressed: () {
                counterBloc
                    .add(CounterEvent(value: 5, status: EventStatus.DECREMENT));
              },
              child: Icon(Icons.remove_circle),
            ),
          ],
        ),
      ),
    );
  }

make sure to init your bloc in the main :


class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BlocProvider<CounterBLoC>(
        create: (ctx) => CounterBLoC(),
        child: TestBlocWidget(),
      ),
    );
  }
}

Upvotes: 8

Ganesh Bhat
Ganesh Bhat

Reputation: 256

If you are trying to rebuild the Counter App using bloc patter, Go through this article this may help.

https://bloclibrary.dev/#/fluttercountertutorial?id=counter-app

Upvotes: 0

Related Questions