Reputation: 3
Im a new flutter developer and have just started using the initState(). However, it seems like it never gets called, I tried all fixes I found on here, but none of them works! Here is a simplified example
import 'package:flutter/material.dart';
class example extends StatefulWidget {
@override
_State createState() => _State();
}
class _State extends State<example> {
@override
void Initstate() {
super.initState();
print('hello world');
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
The hello world never gets printed though!
Upvotes: 0
Views: 1830
Reputation: 11984
initState is not supposed to be inside the build function. You can create the initState function by going to the line above @override before build(), type init, hit ctrl space and it will autocomplete. This would be the correct version:
class _State extends State<Example> {
@override
void initState() {
super.initState();
print('hello world');
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
Upvotes: 1