Catch Exceptions in Flutter Like Never Before!
Вставка
- Опубліковано 26 жов 2022
- In this tutorial, learn how to efficiently catch and handle exceptions in your Flutter App. I will demonstrate some of the possible techniques one could have taken, problems with them and how our solution encounters all of these problems and makes our lives easier.
📌 Discord Server: / discord
Special Thanks: @aadhiarun2230
📕 Additional Resources:
FPDart vs Dartz: github.com/SandroMaglione/fpd...
More on FPDart: www.sandromaglione.com/techbl...
Andrea's FPDart Guide: codewithandrea.com/articles/f...
Resocoder's Dartz Guide: resocoder.com/2019/12/14/func...
Connect With Me Here:
Instagram: / optimalcoding
GitHub: github.com/rivaanranawat
Linkedin: / rivaan-ranawat
Facebook: / rivaan.ranawat
Mail: namanrivaan@gmail.com
Medium: / namanrivaan
Twitter: / ranawatrivaan
#flutter #dart
I would define EitherUser as Either and FutureEitherUser as Future, so that Future is in the name. Or start using Task and those would be TaskEither instead.
Thanks for the tip, Randal! Pinned your comment, hope it helps someone.
Starting very simple and walking through a clean explanation step by step. never seen before. immediately subscribed
Great video ! Keep up the great work mate !
Thank-you very much! Can I ask you a question? At minute 04:02 I see a generator in Visual Studio Code (when you click on the lamp to generate the constructor) to generate JSON serialization, toString, copyWith. Is it a plugin? Thanks.
i think its called "Dart Data Class Generator" an extension for VSCode
@@adisonmasih Thank-you very much!
@@adisonmasih Yeah, and DDCG can go all the way from JSON to a nearly usable data class as well. It gets confused on unusual keys that aren't dart identifiers, but otherwise decent.
Will the new dart 3.0 change this fpdart approach? how can we use dart 3.0 to handle this kind of exception ?
Another Excellent one!
Thanks Saabir!
amazingly awesome tutorial
Thanks Yonathan!
Thank you for the wonderful video bro it is very helpful for me and my team, Thanks again bro
I'm glad it was helpful to you and your team Pravin!
Awesome!
Thanks for the video. 👍
My pleasure!
Nice tip😍 thanks 🙏
Thank you too!
Salaam sir
I hope you are doing well.
Kindly make a video on CRUD operation with API, Riverpod, and exception handling.
shesssh thanks sir
Is using tuple still relevant given that we can use Records?
Bro I'm using windows laptop I'm creating project iOS folder inside podfile not comming continuous or stop my project podfile is compulsory or not windows laptop
You can't create iOS Apps with Windows Operating System.
Thank you Mr. Rivaan Ranawat.
My pleasure!
Subscription added🎉
Awesome, thank you!
What do you think about the new UA-cam desktop UI?
Saw a video on this but they haven’t rolled it out everywhere
Pretty cool imo. Always liked rounded corners. The dark black background gives me an improved immersive experience. The thumbnails stand out better. Would have loved getting comment box in the side panel though, similar to what happens in Live Stream.
bro , isleft and isright method is not available 🤷♂ is there any other method to find ,i searched a lot ,not able to find , plz help
Are you using fpdart package?
@@RivaanRanawat no dartz
Hello Rivaan, thanks for putting this great content for free on the internet. I was wondering if you could recommend me some flutter and dart courses online, as I really want to learn but all of the available courses are not very extensive. Thank you.
Vandad's 37 hour Flutter Course on freeCodeCamp might be what you're looking for 😉
@@RivaanRanawat I tried it at the start, but it went very slow. I'll try it out again. Thanks, you're a life-saver!
@@samuelyung1031 I'll be releasing Flutter Beginners Course within a month or so.
A good package for this is also Dartz.
Precisely! But the last update made on the package was 11 months ago so didn't show it in this vid.
@@RivaanRanawat Dartz also has ZERO documentation. fpdart is not only completely documented, it comes with many examples, and a series of tutorials/blog articles. And it was updated *again* only a few days ago. I've migrated three projects from dartz to fpdart now, and am very happy.
@@RandalLSchwartz Oh yes, Forgot about mentioning the documentation. Resocoder's articles helped me use Dartz. Andrea's articles directed my attention towards fpdart and their documentation has been doing well for me in some of my projects.Thanks for letting me know about their documentation changes. Revisited pub.dev, seems they have added a new example of open meteo. Need to try out the package in newer projects as well, to test out the new extension methods they've added.
can you please create peer to peer payment app, with paypal, credicard, visacard and so on please?
very helpful video by the way
Noted, thank you :)
Bhai single vender e commerce app development me ketna cost lage ga
DM me on Discord at rivaanranawat#9083
We can have chat, if this position is still there
Please make a flutter graphql refreshtoken implementation 🙏
In line.
Bro twitter tutorial how many days to take time currect time telmi bro
Should take about a month. Not more than that, if all goes according to plan.
Dear sir can you make a video on how we can create ott platform please sir it's an humble request please 🙏🏻🙏🏻
Noted!
I LOVE YOU BROOooooo😅😅💖💖🙏🙏
When full youtube clone is coming? Please tell.🙏🙏
Can't assure you with the date but it's in line.
Waiting for clones using serverpod as backend
After it's v1 release, for sure!
Bro build a Basic project for beginner and make some videos on widget it will be great
Will make Beginner Friendly projects soon, thanks for the suggestion!
@@RivaanRanawat Thankyou for reaching out to my comment and i think i will learn more from you
First
second
This is very useful
Glad it was!
I am using this pattern too except I actually wrote my own code for it inspired by Rust's Result enum type that has Ok or Err members:
import 'package:meta/meta.dart';
/// Base Result class
///
/// Receives two values [E] and [T]
/// as [E] is an error and [T] is a success.
@sealed
abstract class Result {
/// Default constructor.
const Result();
Ok get _asOk => this as Ok;
Err get _asErr => this as Err;
T get _okValue => _asOk._value;
E get _errValue => _asErr._value;
/// Returns `true` if the result is [Ok].
bool get isOk => this is Ok;
/// Returns `true` if the result is [Err].
bool get isErr => !isOk;
/// Converts from `Result` to `T?`.
///
/// Converts `this` into a nullable value, consuming `this`,
/// and discarding the error, if any.
T? ok() => isOk ? _okValue : null;
/// Converts from `Result` to `E?`.
///
/// Converts `this` into a nullable value, consuming `this`,
/// and discarding the success value, if any.
E? err() => isOk ? null : _errValue;
/// Maps a `Result` to `Result` by applying a function to a
/// contained [Ok] value, leaving an [Err] value untouched.
///
/// This function can be used to compose the results of two functions.
Result map(U Function(T value) cb) =>
isOk ? Ok(cb(_okValue)) : Err(_errValue);
/// Maps a `Result` to `Result` by applying a function to a
/// contained [Err] value, leaving an [Ok] value untouched.
///
/// This function can be used to pass through a successful result while
/// handling an error.
Result mapErr(F Function(E err) cb) =>
isOk ? Ok(_okValue) : Err(cb(_errValue));
/// Returns the provided default (if [Err]), or
/// applies a function to the contained value (if [Ok]),
///
/// Arguments passed to `mapOr` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use [mapOrElse],
/// which is lazily evaluated.
U mapOr(U Function(T value) cb, U defaultValue) =>
isOk ? cb(_okValue) : defaultValue;
/// Maps a `Result` to `U` by applying fallback function `defaultF` to
/// a contained [Err] value, or function `f` to a contained [Ok] value.
///
/// This function can be used to unpack a successful result
/// while handling an error.
U mapOrElse(U Function(T value) cb, U Function(E err) errCb) =>
isOk ? cb(_okValue) : errCb(_errValue);
/// Returns an iterable over the possibly contained value.
///
/// The iterable yields one value if the result is [Ok], otherwise none.
Iterable iter() => isOk ? [_okValue] : const [];
/// Returns the contained [Ok] value, consuming the `this` value.
///
/// Throw an error if the value is an [Err], with an error message including
/// the passed message, and the content of the [Err].
T expect(String msg) =>
isOk ? _okValue : throw ResultUnwrapError(msg, obj: _errValue);
/// Returns the contained [Ok] value, consuming the `this` value.
///
/// Because this function may throw an error, its use is generally
/// discouraged.
/// Instead, prefer to handle the [Err] case explicitly,
/// or call [unwrapOr] or [unwrapOrElse].
T unwrap() => isOk
? _okValue
: throw ResultUnwrapError(
'called `Result#unwrap` on an `Err` value',
obj: _errValue,
);
/// Returns the contained [Err] value, consuming the `this` value.
///
/// Throw an error if the value is an [Ok], with an error message including
/// the passed message, and the content of the [Ok].
E expectErr(String msg) =>
isOk ? throw ResultUnwrapError(msg, obj: _okValue) : _errValue;
/// Returns the contained [Err] value, consuming the `this` value.
///
/// Exceptions if the value is an [Ok], with a custom error message provided
/// by the [Ok]'s value.
E unwrapErr() => isOk
? throw ResultUnwrapError(
'called `Result#unwrapErr` on an `Ok` value',
obj: _okValue,
)
: _errValue;
/// Returns `res` if the result is [Ok], otherwise returns the [Err] value of
/// `this`.
Result and(Result res) => isOk ? res : Err(_errValue);
/// Calls `op` if the result is [Ok], otherwise returns the [Err] value of
/// `this`.
///
/// This function can be used for control flow based on `Result` values.
Result andThen(Result Function(T value) cb) =>
isOk ? cb(_okValue) : Err(_errValue);
/// Returns `res` if the result is [Err], otherwise returns the [Ok] value of
/// `this`.
///
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use [orElse], which is
/// lazily evaluated.
Result or(Result res) => isOk ? Ok(_okValue) : res;
/// Calls `op` if the result is [Err], otherwise returns the [Ok] value of
/// `this`.
///
/// This function can be used for control flow based on result values.
Result orElse(Result Function(E err) cb) =>
isOk ? Ok(_okValue) : cb(_errValue);
/// Returns the contained [Ok] value or a provided default.
///
/// Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use
/// [unwrapOrElse], which is lazily evaluated.
T unwrapOr(T defaultValue) => isOk ? _okValue : defaultValue;
/// Returns the contained [Ok] value or computes it from a closure.
T unwrapOrElse(T Function(E err) cb) => isOk ? _okValue : cb(_errValue);
}
/// `Ok` is a type that represents success and contains a `T` type success
/// value.
@sealed
class Ok extends Result {
const Ok(this._value);
final T _value;
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
return other is Ok && other._value == _value;
}
@override
int get hashCode => _value.hashCode;
@override
String toString() => 'Ok(${_value.toString()})';
}
/// `Err` is a type that represents failure and contains a `E` type error value.
@sealed
class Err extends Result {
const Err(this._value);
final E _value;
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
return other is Err && other._value == _value;
}
@override
int get hashCode => _value.hashCode;
@override
String toString() => 'Err(${_value.toString()})';
}
/// Error thrown by the runtime system when `unwrap` fails.
@sealed
class ResultUnwrapError extends Error {
ResultUnwrapError(
this.message, {
this.obj,
});
final String message;
final Object? obj;
@override
String toString() => obj != null ? '$message: $obj' : message;
}