5. BLoC stands for Business Logic Component.
Bloc is a design pattern created by Google to help separate
business logic from the presentation layer and enable a
developer to reuse code more efficiently.
41. abstract class DataBlocState {}
class DataBlocInitialState extends DataBlocState {}
class DataBlocLoadingState extends DataBlocState {}
class DataBlocLoadedState extends DataBlocState {
final String data;
DataBlocLoadedState(this.data);
}
class DataBlocNoDataState extends DataBlocState {}
class DataBlocErrorState extends DataBlocState {
final String error;
DataBlocErrorState(this.error);
}
42. class MyDataBloc extends Bloc<DataBlocEvent, DataBlocState> {
MyDataBloc() : super(DataBlocInitialState()) {
on<MyBlocLoadEvent>((event, emit) async {
if (state is DataBlocLoadingState) {
return;
}
emit(DataBlocLoadingState());
try {
final data = await fetchData();
if (data == '') {
emit(DataBlocNoDataState());
} else {
emit(DataBlocLoadedState(data));
}
} catch (e) {
emit(DataBlocErrorState(e.toString()));
}
});
}
} bloc: ^8.1.0
flutter_bloc: ^8.1.1
43. bloc: ^7.2.0
flutter_bloc: ^7.2.0
class MyDataBloc extends Bloc<DataBlocEvent, DataBlocState> {
MyDataBloc() : super(DataBlocInitialState()) {
}
@override
Stream<DataBlocState> mapEventToState(DataBlocEvent event) async* {
if (state is DataBlocLoadingState) {
return;
}
yield DataBlocLoadingState();
try {
final data = await _fetchData();
if (data == '') {
yield DataBlocNoDataState();
} else {
yield DataBlocLoadedState(data);
}
} catch (e) {
yield DataBlocErrorState(e.toString());
}
}
}
47. import 'package:flutter_test/flutter_test.dart';
int sum(int a, int b) {
return a + b;
}
void main() {
test('sum(1, 1) value should be 2', () {
// Arrange
int a = 1;
int b = 1;
// Act
int result = sum(a, b);
// Assert
expect(result, 2);
});
}
主程式
71. Test Double
•Dummy objects are passed around but never actually used. Usually they are just
used to
fi
ll parameter lists.
•Fake objects actually have working implementations, but usually take some
shortcut which makes them not suitable for production.
•Stubs provide canned answers to calls made during the test.
•Spies are stubs that also record some information based on how they were called.
One form of this might be an email service that records how many messages it was
sent.
•Mocks: objects pre-programmed with expectations which form a speci
fi
cation of the
calls they are expected to receive.
https://martinfowler.com/articles/mocksArentStubs.html#RegularTests
86. stream /str'im/
⽔流,⼩河
(Noun.) a natural body of running water
flowing on or under the earth.
https://techcrunch.com/2009/04/27/facebook-opens-up-its-stream-api-to-developers/
https://cdict.net/q/stream
串流
90. abstract class MyBlocState {}
class InitState extends MyBlocState {}
class NumberState extends MyBlocState {
final int number;
NumberState(this.number);
}
定義狀態 (States)
•初始狀態
•取得數字狀態
91. abstract class MyBlocEvent {}
class StartGettingNumberEvent extends MyBlocEvent {}
class StopGettingNumberEvent extends MyBlocEvent {}
定義事件 (Events)
•開始取數字
•停⽌取數字
92. class MyBloc extends Bloc<MyBlocEvent, MyBlocState> {
var numberLoop = NumberLoop();
MyBloc() : super(InitState()) {
on<StartGettingNumberEvent>((event, emit) async {
if (numberLoop.isRunning()) {
return;
}
Stream<int> stream = numberLoop.numberLoop();
await for (var event in stream) {
emit(NumberState(event));
}
});
on<StopGettingNumberEvent>((event, emit) {
numberLoop.cancel();
emit(InitState());
});
}
}