1

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



4

3 回答 3

7

你应该像下面这样写 CounterEvent:

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

并在小部件中使用它们,如下所示:

@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),
            ),
          ],
        ),
      ),
    );
  }

确保在 main 中初始化您的 bloc:


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(),
      ),
    );
  }
}
于 2020-06-24T12:20:06.837 回答
0

考虑制作自定义事件。您的解决方案应该是这样的(没有检查错误,但范式是正确的):

abstract class CounterEvent {}

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

然后在您的集团中:

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;
    }
  }
}
于 2021-04-02T16:04:07.300 回答
0

如果您尝试使用 bloc 模式重建 Counter App,请阅读本文,这可能会有所帮助。

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

于 2020-06-09T05:59:58.937 回答