Snippets Collections
class MySwitcher extends StatelessWidget {
  const MySwitcher({super.key});

  @override
  Widget build(BuildContext context) {
    final status = context.select((MyBloc bloc) => bloc.state.status);
    switch (status) {
      case MyStatus.initial:
      case MyStatus.loading:
        return const Center(child: CircularProgressIndicator());
      case MyStatus.success:
        return const MyView();
      case MyStatus.error:
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('Something went wrong.'),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  context.read<MyBloc>().add(MyDataRequested());
                },
                child: const Text('Try again'),
              ),
            ],
          ),
        );
    }
  }
}
// PAGE
class MyPage extends StatelessWidget {
  const MyPage({super.key});
 
  static String path = 'my';
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // Inject Bloc(s)
      body: BlocProvider(
        create: (context) => MyBloc(
          dependency: context.read<Dependency>(),
        )..add(MyDataRequested()),
        // SWITCHER
        // MySwitcher, and all its nested child Widgets, will have access to MyBloc
        child: const MySwitcher(),
      ),
    );
  }
}
class MyBloc extends Bloc<MyEvent, MyState> {
  MyBloc({required Dependency dependency})
      : _dependency = dependency,
        super(const MyState()) {
    on<MyDataRequested>(_dataRequested);
  }

  final Dependency _dependency;

  FutureOr<void> _dataRequested(
    MyDataRequested event,
    Emitter<MyState> emit,
  ) async {
    emit(state.copyWith(status: MyStatus.loading));
    try {
      final myAsyncData = await _dependency.asyncCall();
      emit(
        state.copyWith(
          myAsyncData: myAsyncData,
          status: MyStatus.success,
        ),
      );
    } catch (error, stackTrace) {
      addError(error, stackTrace);
      emit(state.copyWith(status: MyStatus.error));
    }
  }
}
enum MyStatus { initial, loading, success, error }

class MyState extends Equatable {
  const MyState({
    this.myAsyncData,
    this.status = MyStatus.initial,
  });

  final AsyncData? myAsyncData;
  final MyStatus status;

  @override
  List<Object> get props => [myAsyncData, status];

  // Creates a new state with the changed data keeping the BlocState immutable
  MyState copyWith({
    AsyncData? myAsyncData,
    MyStatus? status,
  }) {
    return MyState(
      myAsyncData: myAsyncData ?? this.myAsyncData,
      status: status ?? this.status,
    );
  }
}
// PAGE
class MyPage extends StatelessWidget {
  const MyPage({super.key});
 
  static String path = 'my';
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // Inject Bloc(s)
      body: BlocProvider(
        create: (context) => MyBloc(dependency: context.read<Dependency>()),
        // VIEW
        // MyView, and all its nested child Widgets, will have access to MyBloc
        child: const MyView(),
      ),
    );
  }
}
star

Thu Oct 06 2022 15:54:44 GMT+0000 (Coordinated Universal Time)

#dart #flutter #bloc #page #view
star

Thu Oct 06 2022 14:58:50 GMT+0000 (Coordinated Universal Time)

#dart #flutter #bloc #page #view
star

Thu Oct 06 2022 14:19:52 GMT+0000 (Coordinated Universal Time)

#dart #flutter #bloc #page #view
star

Thu Oct 06 2022 14:18:34 GMT+0000 (Coordinated Universal Time)

#dart #flutter #bloc #page #view
star

Thu Oct 06 2022 11:22:44 GMT+0000 (Coordinated Universal Time)

#dart #flutter #bloc #page #view

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension