Get Enum from a String in flutter Dart

I wanted to get the correspondent enum value from a string field. Let’s say I have defined enum Fruit.

In the JSON payload of an API call, I’m getting string value of the enum. So I want to convert it to the correct enum value. This is how I did in my flutter application.

 


Restore mongo collection with bson file

I’ve exported the database on the server using mongodump command and dump is stored in .bson file. I need to import that in my local server using mongorestore command.

It’s very simple to import a .bson file:

mongorestore -d db_name -c collection_name /path/file.bson

Incase only for a single collection. Try this:

mongorestore --drop -d db_name -c collection_name /path/file.bson

For restoring the complete folder exported by mongodump:

mongorestore -d db_name /path/

Fine-tune Android Studio with JVM options for smooth experience

android-studio-logo

As android developers we love Android Studio since it make our lives much more easier. If you are a developer form a eclipse (Hats off for the big boss) era I don’t want to say anything. You may feel android studio like a heaven.

With Intelij engine and gradle android development come a long way. But you know, gradle is in fact quire resource hungry guy. Even for a simple “Hello World” application the daemon might use very well up to 150MB or even more. When it comes to multi module production level application we should provide enough resources for gradle.

You may have already experienced slowness or unresponsiveness in android studio with the latest updates (Mostly 3.1.x). When the default memory allocation of android studio  is not enough for your project you have to face this definitely. Only thing we have to do is allocate enough resources.

What you need to do is create a studio.vmoptions file in android studio settings folder. (For 64 bit version it’s   studio64.vmoptions )

Click Help > Edit Custom VM Options to open your studio.vmoptions file. (or you can create the file manually in side bin directory in  android studio installation directory e.g. : C:\Program Files\Android\Android Studio)

Then add below configuration to  studio64.vmoptions file

  • Runs JVM in Server mode with more optimizations and resources usage
    It may slow down the startup, but if you usually keep IDE running for few hours/days
    JVM may profile and optimize IDE better. Feel free to remove this flag.

  • Sets the initial size of the heap, default value is 256m

  • Max size of the memory allocation pool, default value is 1280m

  • Sets the size of the allocated class metadata space that will trigger a GC the first time it is exceeded, default max value is 350m

 

Now your  studio64.vmoptions file should look like this.

Restart the Android Studio and feel the difference.

Happy coding 🙂

 

Credits : https://github.com/artem-zinnatullin/AndroidStudio-VM-Options

Excluding dependencies in Gradle

This is the way that use to exclude some transitive dependency.

When come to project type dependency this is the way.

Keep in mind the additional brackets around  project(project(':library:view_pager_indicator'))



Android – Fix for Duplicate files copied in APK

If you are an android developer you may already familiar with this issue.

Usually you’re getting this for some text files or license files like  META-INF/notice.txt ,  META-INF/license.txt ,  META-INF/ASL2.0

In such scenarios we can exclude those files from APK packaging without any hesitation because we definitely know those will not affect our application functionality. We can do so using gradle  packagingOptions .

What can we do when we get like

or

or

We cannot exclude those files since they required for app functionality. So we need another way to handle such scenarios.

We can use  pickFirst in  packagingOptions to avoid duplicate file copying to APK as below.



Add read more/ less to Android TextView

In this article I’m going to show you how to add  read more/ less component to  android TextView. Finally your text view looks like this.

Screenshot_20170814-014733 (2)



Low Key Portraits of Yash

DSC_0220-1-2

Today I bring you a set of low key portraits of my lovely wife Yash.

I covered all the windows in our living room except one that brings evening sun light to my shots. This beautiful diffused side light emphasized gestures of the face and skin nicely. I planned to produce those photos in back and white but when I see above one in the post processing stage I realize that it looks more attractive with the skin tone. So I exported it as colored one.

You can find the full collection below.



Little Guy Living Under Leafs

 

Today I found this little guy on a flower tree in our garden. Jumping Spider, I totally agree with the name since this one was jumping allover when I tried to take a few clicks. It jumped to the lens several times and I had to put it back on a leaf to continue.

I used my Nikon D5500 DSLR with Nikor 18-55 mm f 3.5-5.6 lens. I put 12 mm and 20 mm extension tubes to achieve extra level of reach. Also I used pop-up flash of the camera to light it up a bit.

Few more clicks are added below.

Is comment required?

Comments are used to describe the code for other users who will reuse or do changes on that code. After few time comments are also very useful even us to realize that what we done in there. So developers used to add comments in everywhere of the code. Is it the best practice?

Add comment isn’t a best practice always. It is better if we can write descriptive code rather than adding comment to describe what we do with the code. If a code can read and get what it done that kind of code says as a descriptive code. Let’s see some example.

or we can change the code as

Which one is more descriptive? In second one it is easy to understand what it done even without a comments. Also it save the time that we wast for adding comments.

But some comments are important and we couldn’t miss. First one is legal comments that contains details about authorship and copyrights. Legal notes are generally added at the start of each source file.

For a example Oracle added Legal note like this in every sources of its products

Also there are some points that we should mention some useful information in comments.

In here we inform the user who refer the code that what do with this property and what can do with change it. At that point comment is very important and it added some value to the code.

Another useful comment is //TODO comments. TODO comment explains what are the features that should implemented but can’t done it now for some reason.

Comments can be use to amplify the important of something that can feel another user as useless.

Javadocs are very important when you are writing public APIs

Rather than that most of the other comments aren’t added much sense on the user except repeating code content to the user. Let’s lock some example comments that we can eliminate from our codes.is

Try to avoid Separator comments because with modern IDEs no use of those separators.

Don’t use comments to keep in track with version histories and author’s details. Modern version controllers manage this task much better. So don’t pollute the code with those things.



Also don’t remain unwanted commented codes in your sources. New comers will not be remove those commented codes unless you remove it. It any code really unwanted delete it without comment it.

When a code is written in first time developer properly organize the code and properly add comments. There is no problem with the code. Problems began when we had to refactor the code urgently as usually happening. Developers quickly done the refactorings and done it in time but most probably they missed the comments to refactor. So there remains irrelevant comments with the code.

Comments are very important but unwanted comments are headache for developers. So think before adding a comment if it’s really useful and be sure it says that actually done.

Next developer only get that we remains on the code. If we didn’t do nicely it’ll be a huge mess for them.