r/FlutterDev • u/serial_dev • Sep 28 '20
Discussion What are the things every Flutter developer should know?
I saw a thread in the r/iOSProgramming subreddit asking what are the things every iOS devs should know. I find the discussions great and (mostly) insightful for new developers.
I was wondering what you think every Flutter/Dart developer should know?
21
u/usbfingers Sep 28 '20
Don't pick a state management library just because everyone says one is better or worse. State management principles have been developed for years in other languages. Assess them for yourself and trust what the larger applications are doing :)
For example, redux doesn't just become bad in Flutter. InvoiceNinja very successfully uses redux in flutter. Additionally, my passion project is a very robust matrix client that cleanly uses redux in flutter as well. After looking at alternatives like bloc and provider, I wouldn't forfeit the control I have for those alternatives.
1
u/paul_h Sep 29 '20
Do you have a perspective on why MVC isn’t possible with Flutter?
1
u/usbfingers Sep 29 '20
My understanding is that you could implement an MVC pattern in flutter. Did you read or see others saying it's not possible? Would love to refute this but have only really used MVC in Android native dev. This article lays out an in-depth implementation of it in Flutter, though i disagree with its comment about redux lol
https://medium.com/follow-flutter/your-next-mvc-flutter-project-1fabe2069b01
I usually discourage MVC because most modern UI frameworks are using MVVM. Many who have been developing frontends over the last 4-5+ years are used to MVVM perhaps even without knowing it. (iOS Swift, react + redux, Vue, etc). This pattern also encourages having domain/business logic entirely separate from the view all together.
Additionally, in my experience, many who use MVC end up writing the business logic inside the controller intertwined with platform specific code (though not the fault of MVC), and you end up having code that is tied to the platform instead of being portable to other projects / frameworks.
Let me know if that helped at all. Happy to answer questions in a DM too
1
u/paul_h Sep 29 '20
I’m a big fan of MVC, going way back. Here’s a demo that’d be easily portable to Flutter & FlutterDriver - https://github.com/paul-hammant/swing_component_testing. I’d love to find someone interested in the exercise and a blog entry experience report following that.
1
u/Rudiksz Sep 29 '20
Additionally, in my experience, many who use MVC end up writing the business logic inside the controller intertwined with platform specific code (though not the fault of MVC), and you end up having code that is tied to the platform instead of being portable to other projects / frameworks.
Your usage of "platform specific code" is weird. My entities and business logic are not related to the platform it is running on. It doesn't matter if my app is runing on Android or iOS or on the web, the business requirements are the same. I don't even know what "platform code" means in the context of separating business logic from presentation.
Both MVC and MVVM offer a way to separate your concerns, but it's up to the developer to follow the rules of each. One is not superior of the other.
1
u/usbfingers Sep 29 '20
I had crafted a response and just lost it on mobile :(
Long story short, I agree. There's nothing necessarily wrong with MVC done right in practice. My experience is anecdotal and what I've seen is many have not been taught how to implement it as the pattern requires, and I've had a much easier time onboarding others to implement MVVM.
To point out an example, the mvc_pattern flutter package actually shows implementing business logic and the model within the controller at first, and then later shows how to create an actual model near the end. Many end up somewhere in between or never getting below that part of the tutorial titled: "How about Model?"
1
u/Rudiksz Sep 29 '20
MVVM is more complex than MVC so I'm surprised to hear that it is easier to pick up. Ironically enough, I'm convinced that the simplicity of MVC is what makes people trip over and have difficulty with it. Because they expect that a large codebase needs convoluted concepts to be able to scale, and since MVC is so dead simple that psychologically they think they must just not understand it. It's ironic really.
New programmers have a tendency of overthinking and overarchitecting their code, and Flutter is not different in this regard. Every new trendy framework will have its fanbase coming up with all sorts of "best practices" and framework and manage to overcomplicate things. It was one of the biggest obstacles to overcome when learning Flutter.
The myriad of state management solutions/articles is a good example. Not coming from the React world, I really struggled with understanding this whole state management issue, until I realized that it's nothing particularly specific to Flutter (or React) and it's so dead simple that it's a total non-issue.
1
u/esDotDev Sep 29 '20
It's not only possible, it's simple and easy :)
https://blog.gskinner.com/archives/2020/09/flutter-state-management-with-mvcs.html1
u/orange_chan Sep 29 '20
Is your project open source by any chance? :D Or do you know other flutter open source projects that use redux? I'll have to look over InvoiceNinja, I only knew about timy messenger.
3
13
u/isakota Sep 28 '20
Same as with anything else:
Use "rubber duck debugging" and pen & paper when you're stuck
Clearly define problem and wanted solution before starting to solve it. Don't change it to fit arhitecture
Simplest solution is usually the best
Devil is in the details
Dont overthink it, just do it, mistakes are best way to learn
Have fun
9
u/dander11 Sep 28 '20
Don't treat every problem as a "Flutter" problem. There are patterns existing for anything you want to develop that can be pretty language agnostic.
Find the best pattern and make Flutter work for you, don't implement some convoluted pattern just because there was a Medium article with "Flutter" in the title.
5
u/Cladser Sep 28 '20
“Just because there was a medium article with flutter in the title’.... that cut deep my friend!
8
7
u/paul_h Sep 28 '20
FlutterDriver may be a dead end. See https://github.com/flutter/flutter/issues/7474
2
u/flagellant Sep 29 '20 edited Aug 09 '24
repeat pocket unite physical compare fragile chief soft shocking cause
This post was mass deleted and anonymized with Redact
1
u/paul_h Sep 29 '20
In 2007 Selenium-RC was hard to use. We in the dev team wondered whether we’d ever solve the problems. Selenium-webdriver was the solution - much much more work to make something that’d automate browsers well, and ultimately bring in all browser makers to cooperate, too.
1
u/shuwatto Sep 29 '20 edited Sep 29 '20
Huh, I have not noticed this yet.
And it looks like
flutter_testis not usable for replacingflutter_driver?edit: never mind,
integration_testpackage seems to come to the rescue.
18
u/esDotDev Sep 28 '20
Use RepaintBoundary to prevent your entire screen from re-painting anytime anything animates.
Also always change your linter file and add:
implicit-casts: false
implicit-dynamic: false
This will force you to write code that the compiler can better understand, and will flag way more errors for you.
5
u/Gbett Sep 28 '20
RepaintBoundary is really needed with lot of animations, I just discovered that recently!
2
u/SladeBrockett Oct 15 '20
I know I'm chiming in here late... but the implicit-casts / dynamic change made a huge improvement in my coding. It was a little difficult to get it write, at first, but man did it yield results!
1
u/esDotDev Oct 15 '20
Right? That moment you realize you've had like 100 potential blind spots in your compiler the whole time... YIKES! Took me a while to realize how bad something like
Function fooreally is,void Function() foois miles better.
5
6
19
u/adel_b Sep 28 '20
- Dart is good language, abuse it but keep code clean
- move any encode / decode off main thread no matter how trivia it is
- 99% of time stateful widget is better than your fav mega state manager
- I have not seen any body using it, `library`, `part` `part of` are godly god for project structure
29
u/a-rns Sep 28 '20
Good luck with 99% StatefulWidget on bigger app 👏 Happy coding 😁
-9
u/adel_b Sep 28 '20
Yes, I prefer to keep widgets self contained, I follow Unix principles here... I use provider where it make sense
1
u/a-rns Sep 28 '20
I recommend you use
ValueNotifierinsteadsetState()and Provider where it make sense for you.1
u/adel_b Sep 28 '20
I don't know you but I don't want to touch your code.
4
u/Rudiksz Sep 28 '20
He is confused about the Flutter internals. That's understable if somebody is new to programming in general or Flutter, we all have to start somewhere. The problem is with this mindless parroting of urban legends and misconceptions started long time ago by some "eminent" package authors.
3
0
Sep 28 '20
What is value notifier?
Why is it better?
-2
u/a-rns Sep 28 '20 edited Sep 28 '20
Read here about ValueNotifier. And it's better for performance becouse rebuild singles widgets in the widget tree which you put inside the
Valuelistenablebuilder. Example:StatefulWidgetrebuild 100% widgets class on setState. AndValueNotifieryou can use globally.7
u/Rudiksz Sep 28 '20
You're very confused and actually giving misleading information. ValueListenableBuilder is a StatefulWidget. ValueListenable literally holds a field in its State object called value, sets up a listener to the ValueNotifier you pass to it, updates its value field when notified and calls setState on itself.
iI's literally just syntactic sugar to save you some boilerplate . The actual source code is like 50 lines of initState/didUpdateWidget/dispose boilerplate.
void _valueChanged() { setState(() { value = widget.valueListenable.value; }); }You can write your own small, self-contained StatefulWidgets that will work exactly like a ValueListenableBuilder performance-wise and call setState on them without worrying about performance.
On the other hand if you call setState inside a monolithic StatefulWidget of course you'll end up hurting performance. But that is true with ValueListenableBuilder too: if it has a monolithic build function then it'll just rebuild the whole damn thing.
You should look at the Flutter source code before parroting these "urban legends".
-1
u/a-rns Sep 28 '20 edited Sep 28 '20
You are right everyone code how they want. StatefulWidget is good for single Widget and force you write boilerplate code, Valuelistenablebuilder you force write cleaner, but personally I used ValueNotifier with Provider to keep state when I needed but example for hide and show obscured text use StatefulWidget is to boilerplate for me. And what is good with ValueNotifier you can use it globally. I have nothing vs StatefulWidget I just use my state in another way. And you have your own coding style I have my own. And don't need to say that I spread wrong information. Probably you can explain better than me.
-2
u/mannprerak Sep 28 '20
ValueNotifier is O(n2) so its recommended to use for 1-2 listeners only
6
1
u/Rudiksz Sep 28 '20
Unless you have hundreds of listeners on hundreds of listenables, this metric is totally irrelevant.
1
u/mannprerak Sep 28 '20
https://github.com/flutter/flutter/pull/62330 Yeah I had similar thoughts, but take a look at these benchmark.. not sure how accurate they are.
2
u/Rudiksz Sep 28 '20
They are probably accurate and I'm not denying that there's optimizations that can be done. It's not even about the ValueNotifier itself but the underlying list/map implementation it uses to store and look up those listeners.
Flutter even has two different implementations optimized for different use cases, it just happens that the ValueNotifier uses one that is optimized for few listeners. If you need a ValueNotifier that needs to store and retrieve dozens of listeners you can just write your own version of it (by creating a copy and swapping out the list implementation)
What I'm saying is it isn't really a concern because most usecases would not require more than 10 listeners, and this "O(n2)" soundbyte should be the deciding factor for using ValueNotifiers or not.
1
u/a-rns Sep 28 '20
Provider also use O(n2) But that doesn't disturb from being the most popular packages.
1
u/mannprerak Sep 28 '20
I never said that, but its use case dependant, the general use case is 1-2 listeners only. but try having 10 listeners and you'll see the performance impact with ChangeNotifier
4
u/SaltTM Sep 28 '20
move any encode / decode off main thread no matter how trivia it is
explain this one for me
7
u/adel_b Sep 28 '20
When you decode / encode json, images or whatever on main thread, you block ui from painting till your operation is complete, moving to isolate make your 120 fps possible
2
u/eRatiosu Sep 28 '20 edited Sep 28 '20
How would I move it to away from the main Thread ?
6
1
u/PinkyWrinkle Sep 28 '20
Can you use isolates with AngularDart?
1
u/adel_b Sep 28 '20
You have to check if web worker is available, however on web this is less of problem, use future instead
5
u/a-rns Sep 28 '20
Encode / decode you use with Future async and await It does not affect performance. Don’t listen to what he says. Answer you can find in the Cookbook.
2
u/miyoyo Oct 03 '20
Read up on how async works, async does not mean blocking tasks magically become infinitely performant, async just means that, if your code is waiting on something to happen, and is otherwise doing nothing else, it might load some other code until that something completes.
Making an async function with an infinite loop is not going to magically make the loop disappear.
1
u/a-rns Oct 03 '20
You not understood my answer. Of course async waiting on something will happen and that why not effect performance.
2
u/miyoyo Oct 03 '20
Encoding/Decoding is pure dart code, that is not an "external" process, that is logic that the app has to spend time running.
I could agree with many examples, but encoding/decoding is specifically a bad example, as it's one of the most common reasons people switch off async and go into threads.
2
u/chrabeusz Sep 29 '20 edited Sep 29 '20
Careful with 'part', it will make code analysis slow as fuck. I recently generated swagger files for big API, which uses part for everything, and it was still analyzing when I killed it after 10 minutes.
2
3
u/dan-danny-daniel Sep 28 '20
my tip would be: learn and discover the niche widgets that are uncommonly used but help a lot. like the intrinsic height widget, the aspect ratio widget, and more. also learn how to use flex widgets effectively.
2
u/JohnGu9 Sep 29 '20
Context is your friend and treat it better. Because you already have Context, flutter no need to state manage.
Do all your work in your widget scope and split work as small as possible for performance and your work time.
1
u/dhaladitya108 Oct 01 '20
Well, I am not a flutter developer, but working as intern. I am just a 2nd year college student.
This are the following things according to me which are required to know:-
bloc pattern
connecting to a rest API
many a times the UI which we want to create is difficult to achieve with the predefined widgets so, we should be able to your own customized widgets
-12
u/danielolaviobr Sep 28 '20
Learn a good state management tool and never go back. I use Get and haven't used a stafeful widget for month now
12
u/interestme1 Sep 28 '20
This is definitely misguided. There shouldn't be a goal to avoid using a basic piece of the framework you're using. Not using stateful widget for a month sounds more like you're trying to jam all sorts of pegs into the same hole rather than accomplishing some cleaner code design.
16
Sep 28 '20
State Management doesn’t take the place of StatefulWidgets. If you’re using it as such then you’re doing it wrong. Flutter docs discuss the difference between ephemeral state and app state. Ephemeral state exists in stateful widgets. It’s overkill to move all state somewhere else.
6
Sep 28 '20
This! Ephemeral state (e.g. form data) doesn’t need to belong in global state. Not having played around with Flutter much, this sounds analogous to React’s localState and libraries like Redux? New devs having just learned Redux often end up moving ALL state into global state, time travelling all the things is cool, but isn’t necessary. Trend in React is to separate state further, local, global and now separately managing state for asynchronous data with tools like “react-query”, making global state even lighter.
-9
u/danielolaviobr Sep 28 '20
Get is not only a state management tool, it is a entire framework bin itselft. I can and it's recommended to use it to substitute stafeful widgets. Please take a look at Get docs to understand more how it works.
2
u/Rudiksz Sep 28 '20
I'm not sure you understand how Get actually does what it does.
The "Get" widgets that you use are either Stateful or StatelessWidgets behind the curtains. You just don't know it, because it's hidden away by the library and the author decided for you which one is better to use.
it's recommended to use it to substitute stafeful widgets
Recommended by whom? The authors of Get, who have the same misconceptions as you?
If you look at the Flutter docs, they describe situations where using StatefulWidgets is prefereable over StatelessWidgets - because there are and I have enoucntered them.
1
u/Fienases Sep 28 '20
This is the situation where they thought i never use any stateful widget because i use the library and it use it for me
-2
u/uV3324 Sep 28 '20
A post much needed. Very useful information shared by the community. My tips:
If stuck and can't debug further on your virtual device, Connect your mobile device and try there.
A good state management employed always helps to scale and for a longer run.
Asks your queries on YouTube, Medium, StackOverflow, Reddit, but also try to share your answer and thoughts where ever possible.
55
Sep 28 '20
[removed] — view removed comment
11
Sep 28 '20
Angela Yu 🙌 I did a course before hers, I thought I was clued up, but did she open my eyes to what I didn't know. 10/10 recommend
0
5
u/zyllus17 Sep 28 '20
Thanks for recommending my Ultimate Flutter Extension Pack.
Here are some of my recommendations -
Follow this Flutter Roadmap .It contains a comprehensive guide of how you should approach Flutter.
Check this repo Flutter Examples before implementing any feature such as signup, splashscreen etc
Check this repo Flutter UI Designs & Flutter UI Kit before implementing any UI design.
If you can donate, donate to the creators of the packages who create and maintain it for free and if you can't then just throw a like or drop a Thank you. That means a lot to the creators
5
u/Arvoci Sep 28 '20
Acedemind is the best course
5
u/iamagoatm8 Sep 28 '20
Haven't tried Angela's course but Academind is really good for beginners, he goes through most basic concepts of programming so you don't even need prior programming knowledge to learn flutter development
3
u/Sir_Cadillac Sep 28 '20
I actually switched from him over to Angela, as I lost track now and then.
92
u/[deleted] Sep 28 '20 edited Sep 28 '20
Dont rely too much on hot reload/restart. Sometimes a "cold" restart can resolve a problem you are having.
Split everything into smaller widgets that can be reused easily. No giant widget trees.
Initstate can and will be called multiple times, potentially loading your data twice etc.*
Decide if you are an app or a webapp and focus on that.
*Possibly depends on how you use it, see comments