This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A simple helper function to allow us to immutably add to lists. | |
extension ImmutableList<T> on List<T> { | |
List<T> concat(T item) => List<T>.from(<T>[...this, item]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// For widgets to be able to read providers, we need to wrap the entire | |
// application in a "ProviderScope" widget. | |
// This is where the state of our providers will be stored. | |
runApp(ProviderScope(child: App())); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We create a "provider", which will store a reference to NotesController. | |
final notesProvider = StateNotifierProvider((ref) => NotesController()); | |
class NotesController extends StateNotifier<NotesState> { | |
NotesController() : super(NotesState.initial()); | |
void addNote() { | |
var notes = state.notes.concat(state.input); | |
state = state.copyWith(notes: notes, input: ''); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseCommand<T> { | |
BuildContext _context; | |
BaseCommand(this._context); | |
T locate<T>(LogicRef<T> ref) => _context.use(ref); | |
T read<T>(StateRef<T> ref) => _context.read(ref); | |
Future<T> run(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:get_it/get_it.dart'; | |
abstract class BaseCommand<T> { | |
GetIt getIt = GetIt.instance; | |
D locate<D>() => getIt.get<D>(); | |
Future<T> run(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseCommand<T> { | |
BuildContext _context; | |
BaseCommand(BuildContext context) { | |
/// Get root context | |
/// If we're passed a context that is known to be root, skip the lookup, it will throw an error otherwise. | |
_context = (context == _lastKnownRoot) ? context : context.read(); | |
_lastKnownRoot = _context; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CounterCubit extends Cubit<int> { | |
CounterCubit() : super(0); | |
void increment() => emit(state + 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CounterEvent { increment } | |
class CounterBloc extends Bloc<CounterEvent, int> { | |
CounterBloc() : super(0); | |
@override | |
Stream<int> mapEventToState(CounterEvent event) async* { | |
switch (event) { | |
case CounterEvent.increment: | |
yield state + 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CounterPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Counter')), | |
body: BlocBuilder<CounterCubit, int>( | |
builder: (context, count) => Center(child: Text('$count')), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: const Icon(Icons.add), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_hooks/flutter_hooks.dart'; | |
import 'package:state_notifier/state_notifier.dart'; | |
T useStateNotifier<T>(StateNotifier<T> notifier) { | |
final state = useState<T>(null); | |
useEffect(() { | |
return notifier.addListener((s) => state.value = s); | |
}, [notifier]); |