Solution for initstate not executing before building widget
is Given Below:
I am trying to get json data from OMDb but my initstate function is not exicuting i figure it out by using print statement in side initsate funtion, but i dont know why it is not showing movies.statuscode
void main() {
print("runned");
runApp(MovieApp());
}
class MovieApp extends StatefulWidget {
const MovieApp({Key? key}) : super(key: key);
@override
_MovieAppState createState() => _MovieAppState();
}
class _MovieAppState extends State<MovieApp> {
@override
void initstate() {
super.initState();
print("pass");
loaddata();
}
loaddata() async {
String url = "https://www.omdbapi.com/?s=Batman&page=2&apikey=2c8219b0";
final movies = await http.get(Uri.parse(url));
print(movies.statusCode);
}
You have a typo. initstate
is not equal to initState
. Try this:
void initState() {
super.initState();
print("pass");
loaddata();
}
Btw loaddata
should also be by convention loadData
.
If you get data from API please go to my answer here or here and add your api URL like this http://www.omdbapi.com/?s=Batman&page=2&apikey=2c8219b0
Create API call function
Future<List<dynamic>> getJobsData() async {
String url="http://www.omdbapi.com/?s=Batman&page=2&apikey=2c8219b0";
var response = await http.get(Uri.parse(url), headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
});
print(response.statusCode);
print(response.body);
return json.decode(response.body)['search'];
}