When click drawer widget rebuild and value variable change to null
I'm using Riverpod and I don't understand why when I click on the drawer, the widget rebuilds and the variable in my SearchNotifier class becomes null.
Here is my Provider
class SearchNotifier extends StateNotifier<SearchResult> {
SearchResult? item = SearchResult();
String? authToken;
SearchNotifier() : super(SearchResult());
SearchResult get getSearchResult {
if (this.item != null)
return this.item!;
else
return SearchResult();
}
void setToken(String token) {
this.authToken = token;
}
Future<void> doSearch(SearchObject searchObject) async {
var logger = Logger();
//logger.d(searchObject.page);
final url = Uri.parse(Constants.API_SEARCH);
try {
final response = await http.post(
url,
body: json.encode(searchObject.toJson()),
headers: Constants.headerWithAuth(this.authToken!),
);
final responseData = json.decode(response.body.toString());
//logger.d(responseData['foods'][0]['images'][0]);
if (responseData['response_code'] != "0") {
throw HttpException(responseData['response_message']);
}
SearchResult searchResult = SearchResult.fromJson(responseData);
if (searchResult.current_page != null && searchResult.current_page! > 1) {
state.copyWith(
current_page: searchResult.current_page,
first_page: searchResult.first_page,
last_page: searchResult.last_page,
next_page: searchResult.next_page,
out_of_range: searchResult.out_of_range,
prev_page: searchResult.prev_page,
total_pages: searchResult.total_pages,
);
state.foods = [...state.foods!, ...searchResult.foods!];
} else {
state = searchResult;
}
} catch (error) {
throw error;
}
}
}
final searchProvider =
StateNotifierProvider<SearchNotifier, SearchResult>((ref) {
final searchNotifier = SearchNotifier();
searchNotifier.setToken(ref.watch(authProvider).token!);
return searchNotifier;
});
here is my a part of Screen code:
class SearchScreen extends ConsumerStatefulWidget {
const SearchScreen({
super.key,
required this.lat,
required this.lng,
});
static const routeName = '/search';
final String lat;
final String lng;
@override
ConsumerState<SearchScreen> createState() => _SearchScreenState();
}
class _SearchScreenState extends ConsumerState<SearchScreen> {
var _isInit = true;
var _isFirstLoading = true;
var _isLoadMore = false;
var _isLastPage = false;
var _currentPage = 0;
var _nextPage;
var _listViewController = ScrollController();
var _gridViewController = ScrollController();
var _textSearchController = TextEditingController();
bool viewType_IsListView = true;
@override
void didChangeDependencies() {
if (_isInit) {
_listViewController = ScrollController()..addListener(loadMore);
_gridViewController = ScrollController()..addListener(loadMoreGridView);
}
_isInit = false;
super.didChangeDependencies();
}
@override
void initState() {
// TODO: implement initState
getData();
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
_listViewController.removeListener(loadMore);
_gridViewController.removeListener(loadMoreGridView);
super.dispose();
}
void loadMore() {
if (_isLastPage == false &&
_nextPage != null &&
_isLoadMore == false &&
_listViewController.position.extentAfter < 300) {
getData(_nextPage);
}
}
void loadMoreGridView() {
if (_isLastPage == false &&
_nextPage != null &&
_isLoadMore == false &&
_gridViewController.position.extentAfter < 300) {
getData(_nextPage);
}
}
void getData([int page = 0]) {
setState(() {
_isLoadMore = true;
if (page > 0) {
_isLoadMore = true;
}
});
DateTime now = DateTime.now();
String formattedDateTime =
DateFormat('dd-MM-yyyy HH:mm \'SGT\'').format(now);
SearchObject searchObject = SearchObject(
service_type: null,
booking_date_time: null,
query: _textSearchController.text,
page: page,
filters: null,
sort_by: null,
// current do not pass lat lng for test
// location_latitude: double.tryParse(widget.lat),
// location_longitude: double.tryParse(widget.lng),
);
// new SearchObjectFilters(
// price_range: '0-999',
// quantity: 0,
// meal_type: [],
// amenities: [],
// cuisines: [],
// dietary_needs: [],
// )
// print('search ' + widget.lat);
// print('search ' + widget.lng);
ref.read(searchProvider.notifier).doSearch(searchObject).then((_) {
setState(() {
_isFirstLoading = false;
_isLoadMore = false;
});
});
}
void getEviroment() async {
await ref.read(environmentVariablesProvider.notifier).getPrefs();
}
void changeViewType(bool viewType) {
setState(() {
viewType_IsListView = viewType;
});
}
@override
Widget build(BuildContext context) {
var searchResults = ref.watch(searchProvider);
print('Run here!');
print(searchResults.foods);
if (searchResults.last_page != null && searchResults.current_page != null) {
setState(() {
_isLastPage = searchResults.last_page!;
_currentPage = searchResults.current_page!;
_nextPage = searchResults.next_page;
});
} else {
setState(() {
_isLastPage = false;
if (searchResults.current_page != null) {
_currentPage = searchResults.current_page!;
}
_nextPage = searchResults.next_page;
if (_nextPage == null) {
_isLastPage = true;
}
});
}
return Scaffold(
drawer: const MainDrawer(),
appBar: AppBar(
Here will be null after click drawer. First time load screen I have a data.
Why is it null?
Comments
Post a Comment