This file contains hidden or 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
final List<CharacterModel> characterList = ref.watch(searchProvider).characterList; |
This file contains hidden or 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
Consumer( | |
builder: (context, ref, child) { | |
return IconButton( | |
onPressed: () { | |
if (textfieldController.text.isEmpty) { | |
ref.refresh(searchProvider); | |
} else { | |
ref.refresh(searchProvider); | |
ref.read(searchProvider.notifier).mapEventsToState( | |
SearchedTextChanged( |
This file contains hidden or 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() { | |
runApp( | |
ProviderScope( | |
child: Sizer( | |
builder: (context, orientation, deviceType) { | |
return const AppWidget(); | |
}, | |
), | |
), | |
); |
This file contains hidden or 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
final searchProvider = StateNotifierProvider.autoDispose<SearchNotifier, SearchState>( | |
(ref) { | |
final characterModelList = ref.watch(charactersDatasProvider).whenOrNull(data: (data) => data); | |
return SearchNotifier() | |
..mapEventsToState( | |
UpdateListItems( | |
characterModelList: characterModelList ?? [], | |
), | |
); |
This file contains hidden or 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 SearchNotifier extends StateNotifier<SearchState> { | |
SearchNotifier() : super(SearchState.empty()); | |
void mapEventsToState(SearchEvent event) { | |
event.map( | |
searchedTextChanged: (searchedTextChangedEvent) { | |
final characterList = [...state.characterList]; | |
final searchedCharacterList = characterList | |
.where((characterModel) => characterModel.characterName | |
.toLowerCase() |
This file contains hidden or 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
part 'search_event.freezed.dart'; | |
@freezed | |
class SearchEvent with _$SearchEvent { | |
const factory SearchEvent.searchedTextChanged({required String text}) = SearchedTextChanged; | |
const factory SearchEvent.updateListItems({required List<CharacterModel> characterModelList}) = UpdateListItems; | |
} |
This file contains hidden or 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
part 'search_state.freezed.dart'; | |
@freezed | |
class SearchState with _$SearchState { | |
factory SearchState({ | |
required List<CharacterModel> characterList, | |
}) = _SearchState; | |
const SearchState._(); | |
factory SearchState.empty() => SearchState( |
This file contains hidden or 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
part 'character.freezed.dart'; | |
@freezed | |
class CharacterModel with _$CharacterModel { | |
factory CharacterModel({ | |
required String characterName, | |
required String characterImgUrl, | |
}) = _CharacterModel; | |
const CharacterModel._(); |
This file contains hidden or 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
final charactersDatasProvider = FutureProvider.autoDispose<List<CharacterModel>>( | |
(ref) { | |
return ref.read(apiProvider).getCharactersDatas().then( | |
(characters) { | |
final List<CharacterModel> characterList = []; | |
for (var i = 0; i < 10; i++) { | |
characterList.add( | |
CharacterModel( | |
characterName: characters[i]["name"], |
This file contains hidden or 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
final apiProvider = Provider((ref) => ApiService()); |