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
Call provider function in initstate #183
Comments
Have you tried doing what the error suggests? |
this is my full code: main.dart
MainUI.dart
DataProvider.dart
i don't know why Provider.of(context, listen: false).getData(); not working in initstate and give me an error |
@rrousselGit gave the answer, you're inserting it in the wrong place. Your initState is calling a function on a Provider that hasn't been built yet. You need to create the provider and then call that function later down the widget tree in a different initstate. |
Try to setup your ChangeNotifierProvider before MaterialApp. |
For anyone who is reading this thread and still doesn't know what to do here is a great explanation: https://pub.dev/packages/provider#faq and the first question:
|
I'd say it is because you are calling a setState() when notifying listeners, and you cant do that when widgets are building. You should this: void initState() { !!REMEMBER TO ADD LISTEN:FALSE |
context is not avaliable in initstate how we can call it in initstate anyway |
It is safe to use Example: @override
void initState() {
super.initState();
var userNotifier = Provider.of<ValueNotifier<UserModel>>(context, listen: false); <-- already has value
controller.text = userNotifier.value.displayName;
} |
@bhanuka96 getting error ...`import 'package:flutter/material.dart'; import 'ViewModel/HomeViewModel/HomeViewModel.dart'; void main() { class MyApp extends StatelessWidget { // This widget is the root of your application.
} ` |
void initState() { |
after using app going crash please help ?? |
@sajibsaha4647 Use this way
use this way If you want to use
|
how i use stateless widget with provider statemanagement |
i call api but i want to not using statefullwidget or inistate method how can i achieve provider api call without initstate method. |
there is an issue when try to call provider function inside initState() method
I'm using it as the following:
@OverRide
the error log is:
can you look into the issue, i've added listen: false but still show error.
Thanks
The text was updated successfully, but these errors were encountered: