spebbe / dartz

Functional programming in Dart
MIT License
749 stars 60 forks source link

After return from Left or Right, still next line is why executing? #51

Closed MuthuHere closed 4 years ago

MuthuHere commented 4 years ago

In my api call i am returning response object in right and error message in left.

image

See i am getting the print line execution. Quite weird image

krevedkokun commented 4 years ago

nothing weird, you're just returning from closure not function

MuthuHere commented 4 years ago

how you mean? any clue ? @krevedkokun

krevedkokun commented 4 years ago

it's hard to tell what you're trying to achieve from that snippet, but my guess is that you should return response.fold

MuthuHere commented 4 years ago

Here you go,

This is a class to return loginResponse once it get success i am returning LoginModel if not i am returning error.

image

spebbe commented 4 years ago

Hi! As @krevedkokun said, you need to return the result of fold. Your current code should produce a warning about not returning a proper value from userLogin – that's probably what the yellow squiggly line is about. This is normal and expected in Dart. The Dart language doesn't have non-local returns.

MuthuHere commented 4 years ago

@spebbe yes, got it but it gives error :-(

image

spebbe commented 4 years ago

I'm not sure what you are trying to do here, but please check your types in your editor of choice, as they do not currently line up correctly. data can never be of runtime type LoginModel. Your should be able to return response.fold(...) directly, although some additional type annotations might be necessary.

I highly recommend adding pedantic or extra_pedantic to your project. They will help you spot a lot of problems statically, including type issues like these.