These are a few of the things I wish someone had shown me when I first started learning Flutter.
To start, if things aren’t working and you aren’t sure why try running flutter clean, it’s kind of a magic fix all.
On Android it can help to manually delete the cache folder inside of bin and on macOS deleting podfile.lock is worth trying.
If you run into any version conflicts a good approach to fix it is to set the version to any (@koorankka), run upgrade again, and if it completes check the pubspec.lock file for the version it resolved to and use it in the pubspec.yaml file.
I also strongly recommend bookmarking the “Workarounds for common issues” page in the Flutter Wiki, in particular the section on pub dependencies.
Related to this if your project fails to open correctly in Android Studio you should be aware of the “Invalidate Caches” option.
Also in Android Studio the keyboard shortcuts (@pblead26) I use constantly are Shift + F10 to run the app, Ctrl + Alt + L to format it and Alt + Enter to show the context menu.
I’m a huge fan of dartfmt, and love how it makes the code consistent. I find it can be helpful to run:
flutter dartfmt lib
To apply formatting across the entire project, in particular this can help clean up future git commits.
A default Flutter project has pretty forgiving linting rules, you can use the sames rules as the Flutter team by downloading the analysis_options.yaml file from the repo. I highly recommend this if you’re new to Dart as it can help reinforce some best practices when getting started.
Quick shout out to Effective Dart, an amazing resource for learning Dart.
In general Flutter’s errors are extremely clear, however the Gradle build errors are sometimes a bit less informative. A useful trick is to open the android folder of the project directly in your IDE and build the app from there, it can sometimes reveal more details about the problem.
fluttersamples.com (@brianegan) is a great site to help understand the many different architecture choices available and awesome-flutter (@BlueAquilae) definitely lives up to its name.
One mistake I continue to make is to mix up the use of parentheses. For example, either of the first two are fine however if you map the property directly to the called function it will cause it to be called repeatedly most likely crashing the app and if you’re really lucky it will take down your IDE with it.
onPressed: onPressed // OK onPressed: () => onPressed() // OK onPressed: onPressed() // VERY BAD!!!
Another common error is dealing with the ‘need layout/need paint’ error in rows and columns. The short answer is try adding an Expanded or Flexible widget, for a more detailed explanation checkout this great series (@scottstoll2017).
Lastly in the ‘mistakes I continue to make’ category is remembering to add .toList() when using a map to general a list of widgets.
I still rely on print statements for debugging more than I’d like to admit but by default the output can be cut off. You can use debugPrint and set a value for wrapWidth to workaround this.
debugPrint(string, wrapWidth: 1000);
Finally, here are few key Flutter links to be aware of. The projects page in the Flutter repo on GitHub provides a great high level view on what the Flutter team are actively working on. This list of links from GitHub is an excellent collection of docs detailing where the project is going in the future and the wiki has a ton of great info.
Let me know if you think I missed anything in the comments and I’ll update the post.
Hope you found this useful! You can follow me on Twitter @hillelcoren or checkout some of the projects I’m working on:
- Invoice Ninja – Open source invoicing (Flutter Web demo)
- It’s All Widgets! – App listing for Flutter
- mudeo – Collaborative music app
Update: here are some additional suggestions from the community:
- If you see Gradle build errors which mention ‘androidx’ check that your app has been migrated. (source).