bloc

    0

    0

    In the code, the class UserBloc extends the Bloc class. The constructor sets the state to be the same as the UserState class. The on methods are called whenever an event occurs for the users in the block. The onAddUser method is called when someone new is added to the block, and the onUpdateUser method is called when someone is updated in the block.

    import 'user_state.dart';
    import 'dart:async';
    import '../../models/User.dart';
    part 'user_event.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    import 'package:equatable/equatable.dart';
    
    
    
    class UserBloc extends Bloc<UserEvent, UserState> {
      UserBloc() : super(const UserState()) {
        on<AddUsers>(_onAddUser);
        on<UpdateUsers>(_onUpdateUser);
      }
    
      Future<FutureOr<void>> _onAddUser(
          AddUsers event, Emitter<UserState> emit) async {
        final state = this.state;
        emit(UserState(allUsers: List.from(state.allUsers)..add(event.users)));
    
        final item = User(
            id: event.users.id,
            firstName: event.users.firstName,
            phoneNumber: event.users.phoneNumber,
            email: event.users.email);
    
        try {
          print('Succesfully save of user!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1');
        } catch (e) {
          print('OMline user not created: $e !!!!!!!!!!!');
        }
      }
    
      Future<void> _onUpdateUser(
          UpdateUsers event, Emitter<UserState> emit) async {
        final state = this.state;
        final user = event.users;
        final index = state.allUsers.indexOf(user);
        //update Locale storage
        List<User> alluser = List.from(state.allUsers)..remove(user);
        alluser.insert(
            index, user.copyWith(firstName: firstName, lastName: lastName));
      }
    }
    
    
    class UserBloc extends Bloc<UserEvent, UserState> {
      UserBloc() : super(const UserState()) {
        on<AddUsers>(_onAddUser);
        on<UpdateUsers>(_onUpdateUser);
      }
    
      Future<FutureOr<void>> _onAddUser(
          AddUsers event, Emitter<UserState> emit) async {
        final state = this.state;
        emit(UserState(allUsers: List.from(state.allUsers)..add(event.users)));
    
        final item = User(
            id: event.users.id,
            firstName: event.users.firstName,
            phoneNumber: event.users.phoneNumber,
            email: event.users.email);
    
        try {
          print('Succesfully save of user!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1');
        } catch (e) {
          print('OMline user not created: $e !!!!!!!!!!!');
        }
      }
    
      Future<void> _onUpdateUser(
          UpdateUsers event, Emitter<UserState> emit) async {
        final state = this.state;
        final user = event.users;
        final index = state.allUsers.indexOf(user);
        //update Locale storage
        List<User> alluser = List.from(state.allUsers)..remove(user);
        alluser.insert(
            index, user.copyWith(firstName: firstName, lastName: lastName));
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.