My async call is returning before list is populated in forEach loop

This code Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename) async { final file = await File(filename); String contents = await file.readAsString(); User user = User.fromJson(json.decode(contents)); String name = user.NameLast + “, ” + user.NameFirst; print(name); l.add(name); } return l; } returns the list l … Read more

How can we change appbar background color in flutter

Declare your Color: const primaryColor = Color(0xFF151026); In the MaterialApp level (will change the AppBar Color in the whole app ) change primaryColor return MaterialApp( title: ‘Flutter Demo’, theme: ThemeData( primaryColor: primaryColor, ), home: MyApp(), ); and if you want to change it on the Widget level modify the backgroundColor appBar: AppBar( backgroundColor: primaryColor, ),

How to Animate Collapsing Elements in Flutter

If you want to collapse a widget to zero height or zero width that has a child that overflow when collapsed, I would recommend SizeTransition or ScaleTransition. Here is an example of the ScaleTransition widget being used to collapse the container for the four black buttons and status text. My ExpandedSection widget is used with … Read more

Display SnackBar in Flutter

In my case i had code like this (in class state) final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); void showInSnackBar(String value) { _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value))); } but i didn’t setup the key for scaffold. so when i add key: _scaffoldKey @override Widget build(BuildContext context) { return new Scaffold( key: _scaffoldKey, body: new SafeArea( snackbar start … Read more

What’s a good recipe for overriding hashcode in Dart?

The quiver package provides helper functions hash2, hash3, etc., which simplify the task of implementing hashCode, with some assurance that it works properly under the Dart VM and when compiled to JavaScript. import ‘package:quiver/core.dart’; class Person { String name; int age; Person(this.name, this.age); bool operator ==(o) => o is Person && name == o.name && … Read more

How to flatten a List?

The easiest way I know of is to use Iterable.expand() with an identity function. expand() takes each element of an Iterable, performs a function on it that returns an iterable (the “expand” part), and then concatenates the results. In other languages it may be known as flatMap. So by using an identity function, expand will … Read more

How to get timezone, Language and County Id in flutter by the location of device in flutter?

Flutter locales explained First of all, you should understand the difference between system settings and your application settings: System settings – what the system provides to your app as user preferences for your information. Application settings – what you decided (explicitly or implicitly) to be the application locale. You may allow your users to select … Read more