New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to avoid 'FlutterError (setState() or markNeedsBuild() called during build.' #149
Comments
Hi @2shrestha22 . Thank you for opening this issue. Does this example represent your case? It works for me, try it and see if it works for you too. void main() {
runApp(_App());
}
class _App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
),
body: TabBarView(
children: [
ArticleListView(),
ArticleListView(),
ArticleListView(),
],
),
),
),
),
);
}
}
class ArticleListView extends StatelessWidget {
final wpPageServiceIM = RM.injectFuture(
() => Future.delayed(Duration(seconds: 1), () => 1),
);
@override
Widget build(BuildContext context) {
return Column(
children: [
wpPageServiceIM.whenRebuilderOr(
onWaiting: () => CircularProgressIndicator(),
onError: null,
builder: () => Text('${wpPageServiceIM.state}'),
),
],
);
}
}
|
Moving |
Hi @GIfatahTH How to make the injected model available to other widgets using the example you provided above ? |
I am facing the same issue using global injection. Here is my code below.
|
Can you provide some more details? |
I have a Screen with 5 PageViews inside. Each one of the pageview is controlled by a button in the MainScreen Page.
When I perform a set state from the onPressed I am getting this error
|
The issue only happens when I use setState. Since I am using PageViews I kinda need to set the state. I am not finding any solution to this problem since 3 days. Can you please assist ? @amoslai5128 |
Where do you define the "registrationViewStore"? Is its data type a PageController? |
registrationViewStore is define as a global injectable like this
|
Can you share the code of your CustomButton? It seems like it might be related to the onPress |
sure here is the custom button below @amoslai5128
|
I have updated the custom to be a statelessWidget instead. I am still getting the same error
|
Can you try to replace your |
Have you solved the problem? |
@duniapay |
Using Injected Model I am trying to fetch articles from end point. I am using TabBar to switch between different category.
Inside ArticleListView() I am using
wpPageServiceIM.whenRebuilderOr()
. While switching from tabbar (tabbar with same page) it gives exception saying 'FlutterError (setState() or markNeedsBuild() called during build.'How should I overcome this? I have single service to fetch article from end point
final wpPageServiceIM = RM.inject<WpPageService>(() => WpPageService(api: _api.state));
While suing
StateBuilder()
solves the issue.I am new at programming, if my question is silly sorry for that.
The text was updated successfully, but these errors were encountered: