You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have called fetchPost in build method but its give error like provider not initilized.if i add future.delay its working
What is best way to call this method and how.
**Sample code **
import'dart:developer';
import'package:action_shortcut_test/services/common/debouncer.dart';
import'package:action_shortcut_test/services/common/page_state.dart';
import'package:action_shortcut_test/services/models/post_model.dart';
import'package:action_shortcut_test/services/remote/api/api.dart';
import'package:flutter/material.dart';
import'package:flutter_riverpod/flutter_riverpod.dart';
classCustomDropDownNotifierextendsAutoDisposeNotifier<PageState<List<PostModel>>> {
lateList<GlobalKey> itemKeys;
@overridePageState<List<PostModel>> build() {
fetchPost();
itemKeys = [];
returnPageInitialState();
}
/// Fetch postFuture<void> fetchPost() async {
log("fetch initial post");
state =PageLoadingState();
final response =await ref.read(apiClient).get(ApiEndpoints.post);
response.fold((error) {
state =PageErrorState(error);
}, (r) {
final list =
r.map((e) =>PostModel.fromJson(e asMap<String, dynamic>)).toList();
itemKeys =List.generate(list.length, (index) =>GlobalKey());
state =PageLoadedState(list);
});
}
/// get item keysList<GlobalKey> get getItemKeys => itemKeys;
}
final customDropDownProvider =AutoDisposeNotifierProvider<
CustomDropDownNotifier,
PageState<List<PostModel>>>(CustomDropDownNotifier.new);
Expected behavior
It should not be showing error
The text was updated successfully, but these errors were encountered:
The problem you are encountering is the following:
The StateNotifier created a valid State in its Constructor.
The new Notifier does create its first state with build().
In your case, calling 'build() -> fetchPost() -> state' does throw the current exception as you don't have a valid state, your build() method has not finished yet.
Describe the bug
I have called fetchPost in build method but its give error like provider not initilized.if i add future.delay its working
What is best way to call this method and how.
**Sample code **
Expected behavior
It should not be showing error
The text was updated successfully, but these errors were encountered: