1
+ import 'package:collection/collection.dart' show IterableExtension;
1
2
import 'package:dartcarwings/dartcarwings.dart' ;
2
3
import 'package:logging/logging.dart' ;
3
4
@@ -17,31 +18,25 @@ enum LeafType {
17
18
olderJapan,
18
19
}
19
20
20
- LeafSession createLeafSession (LeafType leafType, String username, String password) {
21
+ LeafSession createLeafSession (LeafType ? leafType, String ? username, String ? password) {
21
22
switch (leafType) {
22
23
case LeafType .newerThanMay2019:
23
24
return NissanConnectSessionWrapper (username, password);
24
- break ;
25
25
26
26
case LeafType .olderCanada:
27
27
return NissanConnectNASessionWrapper ('CA' , username, password);
28
- break ;
29
28
30
29
case LeafType .olderUsa:
31
30
return NissanConnectNASessionWrapper ('US' , username, password);
32
- break ;
33
31
34
32
case LeafType .olderEurope:
35
33
return CarwingsWrapper (CarwingsRegion .Europe , username, password);
36
- break ;
37
34
38
35
case LeafType .olderJapan:
39
36
return CarwingsWrapper (CarwingsRegion .Japan , username, password);
40
- break ;
41
37
42
38
case LeafType .olderAustralia:
43
39
return CarwingsWrapper (CarwingsRegion .Australia , username, password);
44
- break ;
45
40
46
41
default :
47
42
throw ArgumentError .value (leafType, 'leafType' , 'this LeafType is not supported yet.' );
@@ -51,8 +46,8 @@ LeafSession createLeafSession(LeafType leafType, String username, String passwor
51
46
abstract class LeafSessionInternal extends LeafSession {
52
47
LeafSessionInternal (this .username, this .password);
53
48
54
- final String username;
55
- final String password;
49
+ final String ? username;
50
+ final String ? password;
56
51
57
52
List <VehicleInternal > _lastKnownVehicles = < VehicleInternal > [];
58
53
@@ -62,8 +57,8 @@ abstract class LeafSessionInternal extends LeafSession {
62
57
void setVehicles (List <VehicleInternal > newVehicles) {
63
58
// keep the last states
64
59
for (final VehicleInternal lastKnownVehicle in _lastKnownVehicles) {
65
- final VehicleInternal matchingVehicle =
66
- newVehicles.firstWhere ((VehicleInternal vehicle) => vehicle.vin == lastKnownVehicle.vin, orElse : () => null );
60
+ final VehicleInternal ? matchingVehicle =
61
+ newVehicles.firstWhereOrNull ((VehicleInternal vehicle) => vehicle.vin == lastKnownVehicle.vin);
67
62
matchingVehicle? .setLastKnownStatus (lastKnownVehicle);
68
63
}
69
64
@@ -85,19 +80,19 @@ typedef ExecutableVehicleActionHandler<T> = Future<T> Function(Vehicle vehicle);
85
80
typedef SyncExecutableVehicleActionHandler <T > = T Function (Vehicle vehicle);
86
81
abstract class LeafSession {
87
82
88
- ExecutionErrorCallback onExecutionError;
83
+ ExecutionErrorCallback ? onExecutionError;
89
84
90
85
List <Vehicle > get vehicles;
91
86
92
87
Vehicle _getVehicle (String vin) =>
93
88
vehicles.firstWhere ((Vehicle vehicle) => vehicle.vin == vin,
94
- orElse: () => throw Exception ('Vehicle $vin not found.' ));
89
+ orElse: (( ) => throw Exception ('Vehicle $vin not found.' )) as Vehicle Function () ? );
95
90
96
91
Future <void > login ();
97
92
98
93
Map <String , String > getAllLastKnownStatus ();
99
94
100
- T executeSync <T >(SyncExecutableVehicleActionHandler <T > executable, String vin) {
95
+ T ? executeSync <T >(SyncExecutableVehicleActionHandler <T > executable, String vin) {
101
96
try {
102
97
return executable (_getVehicle (vin));
103
98
} catch (e, stackTrace) {
@@ -120,17 +115,17 @@ abstract class LeafSession {
120
115
}
121
116
122
117
if (! anyCommandSucceeded && onExecutionError != null ) {
123
- onExecutionError (vin);
118
+ onExecutionError ! (vin);
124
119
}
125
120
}
126
121
127
- Future <T > executeWithRetry <T >(ExecutableVehicleActionHandler <T > executable, String vin) async {
122
+ Future <T ? > executeWithRetry <T >(ExecutableVehicleActionHandler <T > executable, String vin) async {
128
123
try {
129
124
return await _executeWithRetry (executable, vin);
130
125
} catch (e, stackTrace) {
131
126
_logException (e, stackTrace);
132
127
if (onExecutionError != null ) {
133
- onExecutionError (vin);
128
+ onExecutionError ! (vin);
134
129
}
135
130
}
136
131
0 commit comments