为了便于理解flutter bloc 我们先看看他是怎么用的
我们先定义相关的bloc
test_bloc.dart
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:test_flutter/blocs/test_event.dart';
import 'package:test_flutter/blocs/test_state.dart';
class TestBloc extends Bloc<TestEvent, TestState> {
TestBloc() : super(TestState()) {
//获取用户的孩子
on<LoadTestEvent>(
(event, emit) async {
emit(TestSuccessState('Hello'+event.type.toString()));
return;
},
);
}
}
test_event.dart
import 'package:equatable/equatable.dart';
class TestEvent extends Equatable {
@override
List<Object?> get props => [];
}
class LoadTestEvent extends TestEvent {
int type;
LoadTestEvent(this.type);
}
class TestChangeEvent extends TestEvent {
TestChangeEvent() {
print('TestChangeEvent');
}
}
<