Dart Linting Tutorial – Healthy Flutter Codebase with analysis_options.yaml

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
nobody cares about code quality until they feel the consequences of these negligence of course keeping the quality of your code high is absolutely crucial especially when you work on a team every app is built by writing individual statements first so before even discussing high-level design patterns you should start with project cleanliness from the ground up by configuring more advanced linting hello welcome to resew cauldre where you are getting prepares for real app development so that you will get freelance clients or a job and be confident about the apps you built so subscribe and hit the bell to join us on our quest for becoming in demand flutter developers in this tutorial you are going to learn about what the heck even linting is and also how to set it up so that for example you are not going to get only a warning message here whenever you forget to add a required named parameter for example unpressed for a floating action button so we're going to take a look at how you can change the severity of this error message so that it won't be only a warning which does not prevent you from running the app and compiling it but it will be instead a fully blown error with that said be sure to check out the written tutorial from the link in the description where you can find all of the code written in this video links to the libraries and overall go through this lesson at your own pace as you are already familiar with and as you can tell by this squiggly line below the floating action button every flutter project and every dart project for that matter has some linting already enabled the thing is that linking may not always do the best thing for example why are we getting only a warning message when we are made a required named parameter badges doesn't make any sense if we take a look at the output we see that the parameter on press is required and it's only warning us about so how can we make this into an error which will mean that when I stop the app and I am going to run it we aren't gonna be able to just run it simply as that what we want instead is something which happens when you have an emitted positional argument for example a text widget needs to take in a positional argument the string which it displays on the screen and whenever something like this happens that you emit a positional argument you get a fully blown error here which means that you are now gonna be able to run the app well you are gonna be able to but at least you get this nice error pop-up which will make sure that you get notified about the error which you have introduced into your codebase so how can we do something like this also for required named parameters that's where analysis options yamo file comes into play by default flutter projects currently don't have this file pre created so we're gonna have to create it manually so that just create a new file at the root of the project and name it analysis options demo and inside there we can change the severity of the error messages if we take a look at the output here we can see that the parameter is required message has called missing required param and actually after adding our own analysis options that the mo file you can see that this error message has changed its severity from warning to being just an info message we don't get yellow squiggly line but instead we get a blue squiggly line we're going to fix that in just a bit by adding some limiting packages but for now let's stick with this mess which we have created here for a while to change the severity of this single error message we're going to need to modify the analyzer settings and then we're going to specify the severity of errors and the error which we want to modify here is this missing required param and with that we can now set it to error and once we save this announced its options that the mo file you'll be able to see that immediately we have a red file shown in vs code and also this has just turned into a fully blown error message so now what that means that when we try to run the app when we are made the unpress parameter we are going to get an error pop-up as well just like we would get it when we emitted a positional argument and of course you can still sort of arm it required named parameters but you will just have to specify them to be no manually so this still works you cannot just forget to input required name parameter and I think that's how it should be actually in dart by default and Dargis had some really idiotic defaults by default so we're going to fix up dart by changing the analysis options that the mo file manually and the possible severity errors which you can specify inside analysis options diamo are ignore info warning and also error we have just changed the severity of an error which was already present inside our flutter project by default after all we have already gotten a squiggly line below the floating action button class instantiation it just wasn't of the correct type we needed to change it from warning to error but what if we want to enable some linking which is not by default present inside a dart or a flutter project and also how can we find out which linking rules are available out there well the answer to that question is to check out the docs of the official linker package because in dart even linting is handled by a package is not just baked into the dart language by itself it's really a separate package which you can actually check out on pub da def so let's go over here and we are going to search for linter this is a regular package as you can see it's updated quite frequently so let's open that up we're going to go into it repository and of course the link to this is also in the written tutorial which is available on reso coder comm from the link in the description and what we are actually interested in is this github io page for this package so that just open it up and here we can see the list of lint rules for example is there a rule which will prevent the reassignment of a method parameter because I think that is just not cool for example here we have some method which I have created previously and what we can currently do is say X is equal to 5 for example is this something which dart should allow I don't think so parameters should not be reassigned functions come from mathematics and in mathematics you just cannot reassign suddenly a parameter of a function all the function should do is use the parameters but not change them right and as for programming purposes changing the value of parameters just can cause a bunch of problems you are not gonna be able to keep the mental model of your code in mind so you should really not reassign parameters so is there a lint rule which we can enable which will notify us when we do something stupid as this well of course there is let's just look it up on the page so I know that this rule is actually called parameter sign so I'm just going to search for it here and you can of course just scroll through and find rules which you are interested in so here we go parameter assignments as you can see this rule is not enabled anywhere not even in flutter and basically nowhere so let's just take a look at this and you can see that this will prevent us from reassigning parameters so how can we enable this rule well the answer is again analysis options dot gamma so let's jump back in there and we can not just change the severity of this linter rule because it's not even enabled we first need to enable it and then we can go about changing its severity because the default severity of every rule is info and we're gonna want to change it to be a fully blown error again but let's first enable the whole interval itself to do that we're going to specify linter which is again a package so that's why we specify it as the top-level thing in this analysis options demo analyzer is also a package by the way everything is a package in the dark ecosystem basically except for the most common libraries and we want to enable some rules and the rule is parameter assignments alright so when we say that we're going to take a look at main that dart and we can see that now we get a nice squiggly line with a message don't reassign references to parameters of functions or methods so that's cool but wouldn't it be nicer to again have a fully blown error here or at least a warning message of course it would be nicer so we are going to change parameter assignments severity inside the analyzers errors to be an error I and when we do that we again are going to get a red file color in vs code and we can see that which is basically get an error here which is definitely good thing and also one quick mention of the yeah mole style which you can use for the rules we have to specified it as a list of just simple rule names but you can also make these rules element I'm not sure how it's properly called an e mo anyway but you can make this into a map where you can assign true or false so if there are some rules coming from a package or from the default linting which you want to disable you can search their name and then tell them to be false if you want to enable the rule you can set it to true however when you have this map style of rules you cannot mix it with the list style of rules which is always enables the rule in question so you can choose one format or the other but you cannot mix the map and list formats for the rules but let's just make it back into a list and of course you can find all of this in the written tutorial so although looking through the list of all possible linter rules right here can be indeed very enlightening I think everybody has something more important to do than just look through these rules thankfully we can utilize packages which come with some sensible defaults regarding lint rolls and also their severity currently there are three such packages worth mentioning the first one is pedantic this is used internally by Google which is both a blessing and a curse and that's because they don't want to break their code base by enabling certain rules which are otherwise really worth enabling so we are now going to use pedantic but you can use it and it's available on pop dot def actually if we click here we should be taken to pedantic right here the next one is effective dart if we take a look at that this is one is a community package it has rules stemming from the official dart guidelines hence the name effective dart what we are going to use though is a community package called linked it's made by Pascal belch and it enables some sensible defaults and as you can see this package can be used as a replacement for a package pedantic for those who prefer stricter rules so how can we go about enabling rules from third-party package well we of course first need to add it to pub spec yamo so let's do this then we're going to add it under dev dependencies because we of course don't need linking while the app is already running so let's add it to dev dependencies and once we have it as a dependency we can go to analysis options that yamo and usually it's done in the first line of the file we can include that packages analysis options that yamo file so include package link that's its name and we want to include analysis options yamo and to find out about the rules which are enabled by the package and also about their severity levels you can go to the packages github so let's just go there repository and we can check out its lid folder then analysis options that yamo and as you can see here we go we can see everything which is enabled and there's quite a lot going on here and again if you want to override something which the package does you can always disable the rule here by setting it to be false and you can also change the severity level coming from the package to your own liking once we have added this package you can see that a bunch of new issues errors and warnings have popped up for example we of course still cannot reassign a pair matters we're just going to get rid of this mess but now we have a warning here saying that this function has a return type of string but doesn't end with a return statement previously this was just an info message now we get it as a warning what's more interesting though is that we also get warned about useless containers which really have no place here this container does nothing more than just cover up the code base so now we know that this is useless and we can remove it from the widget tree next we were actually notified about this thing even before but let's take a look at it now immutable classes like for example stateless widgets should not have non final fields and actually we also get a message under display text which says private field could be final prefer final fields this one is actually coming from the lint package so now we can use quick fixes by either hitting ctrl + dot in vs code or you can also press this light bulb and we can make this final next up we have squiggle line below text display prefer declare cons constructors on immutable classes which is a good observation so we're going to set it to Const and if you want to learn everything you need to know about cons constructors in dart check out a separate tutorial from the cart in the corner another thing we get with cons constructors because this text display widget now has one we should use it whenever possible so we get this squiggly line below text display saying and we should prefer Const with constant constructors and there is actually a nice quick fix for this so again ctrl + dot and we can add cons modifier just like that so now you have absolutely no excuse not to use cons because you don't even have to employ your brain power into that you can just follow the lint and it's going to tell you where you should use Const keywords now we even get notified that we should sort children as last in widget instance instantiations so move child property to the end of arguments and should you not want this rule to be enabled you can always just copy its name and set it to be false in the rules but I for once quite like this and again this text is also capable of using constant constructors so let's add it nice modifying analysis options that yamo just opens up new possibilities to fix up darts stupid defaults for example this method which has a return type but doesn't return anything right currently it gives us a warning but I would even make it into a fully blown error and with analysis options it's very simple which is need to copy the name missing returned and as you know by now we are just going to change the severity to be error and now absolute garbage of a code like this will not be able to compile without notifying us with a nice pop up because you know most of the real programming languages sorry JavaScript bombard you with an error if you forget about the return statement and now we have just brought this functionality over to dart to go through this tutorial at your own pace once again and to get all of the code check out the written tutorial available from the link in the description and be sure to tweak the rules and their severity to make your code base as healthy as it possibly can be and if you're serious about becoming a great flutter developer who can build real apps for clients or at the job go to flutter that education link is also in the description to get the top curated flutter news and resources aimed at improving your app development career over there you can also subscribe to my mailing list to get the best photo resources delivered weekly right into your inbox and if you don't want to miss more tutorials like this be sure to subscribe to this channel and also join notification squad by hitting the bell button to make sure you grow your flutter skills because here recorder I am determined to provide you with the best tutorials and resources so that you will become an in-demand flutter developer if this video helped you with understanding linting in dart and also how to configure your own lint rolls and their severity give this video a like and also share it with our developers who will surely benefit from it too if you have anything to say leave a comment down below and see you in the next video [Music]
Info
Channel: Reso Coder
Views: 16,409
Rating: undefined out of 5
Keywords: resocoder, tutorial, programming, code, programming tutorial, flutter, flutter tutorial, dart lint, dart analysis options, flutter lint, flutter error, flutter warning, dart tutorial, dart error, dart clean code, flutter clean code, dart code quality, flutter code quality
Id: GABu350IWXk
Channel Id: undefined
Length: 20min 39sec (1239 seconds)
Published: Fri Jan 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.