Is there a way to load async data on InitState method?

You can create an async method and call it inside your initState @override void initState () { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_){ _asyncMethod(); }); } _asyncMethod() async { _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) { setState(() { _currentUser = account; }); }); _googleSignIn.signInSilently(); }

Converting timestamp

Your timestamp format is in fact in Seconds (Unix timestamp) as opposed to microseconds. If so the answer is as follows: Change: var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp); to var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

Why does setState take a closure?

When Flutter had a “markNeedsBuild” function, developers ended up just sort of calling it at random times. When the syntax switched to setState(() { … }), developers were much more likely to use the API correctly. They are functionally equivalent from the machine’s point of view, but they seem to evoke different code from developers. … Read more

When should I use a FutureBuilder?

FutureBuilder removes boilerplate code. Let’s say you want to fetch some data from the backend on page launch and show a loader until data comes. Tasks for ListBuilder: Have two state variables, dataFromBackend and isLoadingFlag On launch, set isLoadingFlag = true, and based on this, show loader. Once data arrives, set data with what you … Read more

What are Keys in the Stateless widgets class?

TLDR: All widgets should have a Key key as optional parameter or their constructor. Key is something used by flutter engine at the step of recognizing which widget in a list as changed. It is useful when you have a list (Column, Row, whatever) of widgets of the same type that can potentially get removed/inserted. … Read more

How to preserve widget states in flutter, when navigating using BottomNavigationBar?

For keeping state in BottomNavigationBar, you can use IndexedStack @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: BottomNavigationBar( onTap: (index) { setState(() { current_tab = index; }); }, currentIndex: current_tab, items: [ BottomNavigationBarItem( … ), BottomNavigationBarItem( … ), ], ), body: IndexedStack( children: <Widget>[ PageOne(), PageTwo(), ], index: current_tab, ), ); }

What is the true meaning of pass-by-reference in modern languages like Dart?

Quick answer: what gets passed to your functions cookEggs and cookOne are references to the objects, not to the variables (which would be real pass-by-reference). The term pass-by-reference is often misused to mean pass-references-by-value: many languages only have pass-by-value semantics, where the values that are passed around are references (i.e. pointers, without the dangerous features). … Read more

How to use Google API in flutter?

This worked for me: Logging in using package google_sign_in and then get auth headers from it: import ‘package:google_sign_in/google_sign_in.dart’ show GoogleSignIn, GoogleSignInAccount; import ‘package:googleapis/people/v1.dart’ show ListConnectionsResponse, PeopleApi; useGoogleApi() async { final _googleSignIn = new GoogleSignIn( scopes: [ ’email’, ‘https://www.googleapis.com/auth/contacts.readonly’, ], ); await _googleSignIn.signIn(); final authHeaders = _googleSignIn.currentUser.authHeaders; // custom IOClient from below final httpClient = GoogleHttpClient(authHeaders); … Read more

How can I compare Lists for equality in Dart?

To complete Gunter’s answer: the recommended way to compare lists for equality (rather than identity) is by using the Equality classes from the following package import ‘package:collection/collection.dart’; Edit: prior to 1.13, it was import ‘package:collection/equality.dart’; E.g.: Function eq = const ListEquality().equals; print(eq([1,’two’,3], [1,’two’,3])); // => true The above prints true because the corresponding list elements … Read more

how to solve flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?

Just for the sake of clarity specially for the newcomers to Flutter/Dart, here is what you need to do in order to enable this option globally in your project: In your main.dart file, add or import the following class: class MyHttpOverrides extends HttpOverrides{ @override HttpClient createHttpClient(SecurityContext? context){ return super.createHttpClient(context) ..badCertificateCallback = (X509Certificate cert, String host, … Read more