initState method

723 views
Skip to first unread message

Kamran Ali

unread,
Aug 17, 2019, 7:19:16 AM8/17/19
to Flutter Dev
I want to initialize data before build method so i override initState method and do my initialization stuff, but when i run the app then build method is called before initState method that's why it gives me the error "The getter 'game' was called on null.". i don't know what to do now. Attached file is my main.dart
main.dart

EthicCoders Apps

unread,
Aug 17, 2019, 7:44:32 AM8/17/19
to Kamran Ali, Flutter Dev
Consider adding bool _loading variable to the app and use it within your main.dart and add do add _buildWrapper(BuildContext context) to show loading progress indicator. Steps below:
class _MyHomePageState extends State<MyHomePage> {
DetailList detailList;
HeaderMatch headerMatch;
bool _loading; // Add this variable

// for Header Match
Future<DetailList> fetchHeader() async {
final response =
await http.get('http://placaresportivo.net:5000/api/demo/fixtures/id/83');

if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON.
setState(() {
headerMatch = HeaderMatch.fromJson(json.decode(response.body));
fetchPost();
_loading = false; // set it to false...
return headerMatch;
});
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
...
.......

@override
void initState() {
setState(() {
_loading = true;
});
this.fetchHeader();
super.initState();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Color(0xff2c2840),
automaticallyImplyLeading: true,
title: Center(
child: appBarWidget(),
),
),
body: _buildWrapper(context) // This trailing comma makes auto-formatting nicer for build methods.
),
);
}

Widget _buildWrapper(BuildContext context) {
if(_loading) {
return Center(child: CircularProgressIndicator(),);
}else {
return bodyWidget();
}
}
...
.....
.......



On Sat, Aug 17, 2019 at 4:49 PM Kamran Ali <kamran....@gmail.com> wrote:
I want to initialize data before build method so i override initState method and do my initialization stuff, but when i run the app then build method is called before initState method that's why it gives me the error "The getter 'game' was called on null.". i don't know what to do now. Attached file is my main.dart

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/CABOCyWOYDGX9gajbuVXepGx9%3DUk8ocvAdNMR73wXWUYL3NMCgA%40mail.gmail.com.

Kamran Ali

unread,
Aug 17, 2019, 7:58:33 AM8/17/19
to EthicCoders Apps, Flutter Dev
👌👍

EthicCoders Apps

unread,
Aug 17, 2019, 8:11:29 AM8/17/19
to Kamran Ali, Flutter Dev
You might want to consider structuring your code... Else in no time the main.dart will 'bloat'...

1) A package/folder for all network calls.
2) Consider using bloc and flush out JSON objects as streams, the bloc internally will call all network calls... 
3) Since JSON is involved can be very helpful in considering JSON serilization, if not done yet... (Can't make out as data models was not attached...) 
4) for all UI - consider adding a folder for pages 
5) utils folder for all utils and const... 
6) etc... 

Kamran Ali

unread,
Aug 17, 2019, 8:16:52 AM8/17/19
to EthicCoders Apps, Flutter Dev
Yeah will do these changes, right now I'm just practicing 
Reply all
Reply to author
Forward
0 new messages