Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
fluttertpc_get
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Jonny Borges
2022-01-03 11:18:59 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
74a5433b599f89223b22b9982457351c705fab06
74a5433b
1 parent
877186a4
append -> futurize
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
50 deletions
example/lib/pages/home/presentation/controllers/home_controller.dart
lib/get_state_manager/src/rx_flutter/rx_notifier.dart
example/lib/pages/home/presentation/controllers/home_controller.dart
View file @
74a5433
...
...
@@ -11,17 +11,12 @@ class HomeController extends StateController<CasesModel> {
@override
void
onInit
()
{
super
.
onInit
();
//Loading, Success, Error handle with 1 line of code
listenFutur
e
(()
=>
homeRepository
.
getCases
);
futuriz
e
(()
=>
homeRepository
.
getCases
);
}
Country
getCountryById
(
String
id
)
{
final
index
=
int
.
tryParse
(
id
);
if
(
index
!=
null
)
{
return
state
.
countries
[
index
];
}
return
state
.
countries
.
first
;
return
index
!=
null
?
state
.
countries
[
index
]
:
state
.
countries
.
first
;
}
}
...
...
lib/get_state_manager/src/rx_flutter/rx_notifier.dart
View file @
74a5433
...
...
@@ -7,7 +7,7 @@ import '../../../instance_manager.dart';
import
'../../get_state_manager.dart'
;
import
'../simple/list_notifier.dart'
;
extension
_
NullOr
Empty
on
Object
{
extension
_Empty
on
Object
{
bool
_isEmpty
()
{
final
val
=
this
;
// if (val == null) return true;
...
...
@@ -25,21 +25,31 @@ extension _NullOrEmpty on Object {
mixin
StateMixin
<
T
>
on
ListNotifier
{
late
T
_value
;
GetState
?
_status
;
GetState
<
T
>
?
_status
;
void
_fill
Empty
Status
()
{
void
_fill
Initial
Status
()
{
_status
=
(
value
==
null
||
value
!.
_isEmpty
())
?
GetState
.
loading
()
:
GetState
.
success
(
_status
);
?
GetState
<
T
>.
loading
()
:
GetState
<
T
>.
success
(
_value
);
}
GetState
get
status
{
GetState
<
T
>
get
status
{
reportRead
();
return
_status
??=
_status
=
GetState
.
loading
();
}
T
get
state
=>
value
;
set
status
(
GetState
<
T
>
newStatus
)
{
if
(
newStatus
==
status
)
return
;
_status
=
newStatus
;
if
(
newStatus
is
SuccessState
<
T
>)
{
_value
=
newStatus
.
data
!;
return
;
}
refresh
();
}
@protected
T
get
value
{
reportRead
();
...
...
@@ -53,33 +63,17 @@ mixin StateMixin<T> on ListNotifier {
refresh
();
}
@protected
void
change
(
T
newState
,
{
GetState
?
status
})
{
var
_canUpdate
=
false
;
if
(
status
!=
null
)
{
_status
=
status
;
_canUpdate
=
true
;
}
if
(
newState
!=
_value
)
{
_value
=
newState
;
_canUpdate
=
true
;
}
if
(
_canUpdate
)
{
refresh
();
}
}
void
listenFuture
(
Future
<
T
>
Function
()
body
(),
void
futurize
(
Future
<
T
>
Function
()
body
(),
{
String
?
errorMessage
,
bool
useEmpty
=
true
})
{
final
compute
=
body
();
compute
().
then
((
newValue
)
{
if
((
newValue
==
null
||
newValue
.
_isEmpty
())
&&
useEmpty
)
{
change
(
newValue
,
status:
GetState
.
loading
()
);
status
=
GetState
<
T
>.
loading
(
);
}
else
{
change
(
newValue
,
status:
GetState
.
success
(
newValue
)
);
status
=
GetState
<
T
>.
success
(
newValue
);
}
},
onError:
(
err
)
{
change
(
state
,
status:
GetState
.
error
(
errorMessage
??
err
.
toString
()
));
status
=
GetState
.
error
(
errorMessage
??
err
.
toString
(
));
});
}
}
...
...
@@ -160,7 +154,7 @@ class Value<T> extends ListNotifier
implements
ValueListenable
<
T
?>
{
Value
(
T
val
)
{
_value
=
val
;
_fill
Empty
Status
();
_fill
Initial
Status
();
}
@override
...
...
@@ -226,36 +220,36 @@ typedef NotifierBuilder<T> = Widget Function(T state);
abstract
class
GetState
<
T
>
{
const
GetState
();
factory
GetState
.
loading
()
=>
GLoading
();
factory
GetState
.
error
(
String
message
)
=>
GError
(
message
);
factory
GetState
.
empty
()
=>
GEmpty
();
factory
GetState
.
success
(
T
data
)
=>
GSuccess
(
data
);
factory
GetState
.
loading
()
=>
LoadingState
();
factory
GetState
.
error
(
String
message
)
=>
ErrorState
(
message
);
factory
GetState
.
empty
()
=>
EmptyState
();
factory
GetState
.
success
(
T
data
)
=>
SuccessState
(
data
);
}
class
GLoading
<
T
>
extends
GetState
<
T
>
{}
class
LoadingState
<
T
>
extends
GetState
<
T
>
{}
class
GSuccess
<
T
>
extends
GetState
<
T
>
{
class
SuccessState
<
T
>
extends
GetState
<
T
>
{
final
T
data
;
GSuccess
(
this
.
data
);
SuccessState
(
this
.
data
);
}
class
GError
<
T
,
S
>
extends
GetState
<
T
>
{
class
ErrorState
<
T
,
S
>
extends
GetState
<
T
>
{
final
S
?
error
;
GError
([
this
.
error
]);
ErrorState
([
this
.
error
]);
}
class
GEmpty
<
T
>
extends
GetState
<
T
>
{}
class
EmptyState
<
T
>
extends
GetState
<
T
>
{}
extension
StatusDataExt
<
T
>
on
GetState
<
T
>
{
bool
get
isLoading
=>
this
is
GLoading
;
bool
get
isSuccess
=>
this
is
GSuccess
;
bool
get
isError
=>
this
is
GError
;
bool
get
isEmpty
=>
this
is
GEmpty
;
bool
get
isLoading
=>
this
is
LoadingState
;
bool
get
isSuccess
=>
this
is
SuccessState
;
bool
get
isError
=>
this
is
ErrorState
;
bool
get
isEmpty
=>
this
is
EmptyState
;
String
get
errorMessage
{
final
isError
=
this
is
GError
;
final
isError
=
this
is
ErrorState
;
if
(
isError
)
{
final
err
=
this
as
GError
;
final
err
=
this
as
ErrorState
;
if
(
err
.
error
!=
null
&&
err
.
error
is
String
)
{
return
err
.
error
as
String
;
}
...
...
@@ -263,4 +257,12 @@ extension StatusDataExt<T> on GetState<T> {
return
''
;
}
T
?
get
data
{
if
(
this
is
SuccessState
<
T
>)
{
final
success
=
this
as
SuccessState
<
T
>;
return
success
.
data
;
}
return
null
;
}
}
...
...
Please
register
or
login
to post a comment