Skip to content

Commit 4a48c2f

Browse files
committed
refactor: support for null saftey. Closes wilburx9#88
1 parent 44ae784 commit 4a48c2f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+765
-1024
lines changed

example/lib/main.dart

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HomePage extends StatefulWidget {
3636
}
3737

3838
class _HomePageState extends State<HomePage> {
39-
final _scaffoldKey = new GlobalKey<ScaffoldState>();
39+
final _scaffoldKey = new GlobalKey<ScaffoldMessengerState>();
4040
final _formKey = GlobalKey<FormState>();
4141
final _verticalSizeBox = const SizedBox(height: 20.0);
4242
final _horizontalSizeBox = const SizedBox(width: 10.0);
@@ -46,12 +46,12 @@ class _HomePageState extends State<HomePage> {
4646
color: Colors.red,
4747
);
4848
int _radioValue = 0;
49-
CheckoutMethod _method;
49+
CheckoutMethod _method = CheckoutMethod.selectable;
5050
bool _inProgress = false;
51-
String _cardNumber;
52-
String _cvv;
53-
int _expiryMonth = 0;
54-
int _expiryYear = 0;
51+
String? _cardNumber;
52+
String? _cvv;
53+
int? _expiryMonth;
54+
int? _expiryYear;
5555

5656
@override
5757
void initState() {
@@ -79,22 +79,20 @@ class _HomePageState extends State<HomePage> {
7979
child: const Text('Initalize transaction from:'),
8080
),
8181
new Expanded(
82-
child: new Column(
83-
mainAxisSize: MainAxisSize.min,
84-
children: <Widget>[
85-
new RadioListTile<int>(
86-
value: 0,
87-
groupValue: _radioValue,
88-
onChanged: _handleRadioValueChanged,
89-
title: const Text('Local'),
90-
),
91-
new RadioListTile<int>(
92-
value: 1,
93-
groupValue: _radioValue,
94-
onChanged: _handleRadioValueChanged,
95-
title: const Text('Server'),
96-
),
97-
]),
82+
child: new Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
83+
new RadioListTile<int>(
84+
value: 0,
85+
groupValue: _radioValue,
86+
onChanged: _handleRadioValueChanged,
87+
title: const Text('Local'),
88+
),
89+
new RadioListTile<int>(
90+
value: 1,
91+
groupValue: _radioValue,
92+
onChanged: _handleRadioValueChanged,
93+
title: const Text('Server'),
94+
),
95+
]),
9896
)
9997
],
10098
),
@@ -105,7 +103,7 @@ class _HomePageState extends State<HomePage> {
105103
border: const UnderlineInputBorder(),
106104
labelText: 'Card number',
107105
),
108-
onSaved: (String value) => _cardNumber = value,
106+
onSaved: (String? value) => _cardNumber = value,
109107
),
110108
_verticalSizeBox,
111109
new Row(
@@ -118,7 +116,7 @@ class _HomePageState extends State<HomePage> {
118116
border: const UnderlineInputBorder(),
119117
labelText: 'CVV',
120118
),
121-
onSaved: (String value) => _cvv = value,
119+
onSaved: (String? value) => _cvv = value,
122120
),
123121
),
124122
_horizontalSizeBox,
@@ -128,8 +126,7 @@ class _HomePageState extends State<HomePage> {
128126
border: const UnderlineInputBorder(),
129127
labelText: 'Expiry Month',
130128
),
131-
onSaved: (String value) =>
132-
_expiryMonth = int.tryParse(value),
129+
onSaved: (String? value) => _expiryMonth = int.tryParse(value ?? ""),
133130
),
134131
),
135132
_horizontalSizeBox,
@@ -139,8 +136,7 @@ class _HomePageState extends State<HomePage> {
139136
border: const UnderlineInputBorder(),
140137
labelText: 'Expiry Year',
141138
),
142-
onSaved: (String value) =>
143-
_expiryYear = int.tryParse(value),
139+
onSaved: (String? value) => _expiryYear = int.tryParse(value ?? ""),
144140
),
145141
)
146142
],
@@ -170,16 +166,14 @@ class _HomePageState extends State<HomePage> {
170166
: new Column(
171167
mainAxisSize: MainAxisSize.min,
172168
children: <Widget>[
173-
_getPlatformButton(
174-
'Charge Card', () => _startAfreshCharge()),
169+
_getPlatformButton('Charge Card', () => _startAfreshCharge()),
175170
_verticalSizeBox,
176171
_border,
177172
new SizedBox(
178173
height: 40.0,
179174
),
180175
new Row(
181-
mainAxisAlignment:
182-
MainAxisAlignment.spaceBetween,
176+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
183177
crossAxisAlignment: CrossAxisAlignment.center,
184178
children: <Widget>[
185179
new Flexible(
@@ -191,21 +185,17 @@ class _HomePageState extends State<HomePage> {
191185
isDense: true,
192186
hintText: 'Checkout method',
193187
),
194-
isEmpty: _method == null,
195-
child: new DropdownButton<
196-
CheckoutMethod>(
188+
child: new DropdownButton<CheckoutMethod>(
197189
value: _method,
198190
isDense: true,
199-
onChanged: (CheckoutMethod value) {
200-
setState(() {
201-
_method = value;
202-
});
191+
onChanged: (CheckoutMethod? value) {
192+
if (value != null) {
193+
setState(() => _method = value);
194+
}
203195
},
204196
items: banks.map((String value) {
205-
return new DropdownMenuItem<
206-
CheckoutMethod>(
207-
value:
208-
_parseStringToMethod(value),
197+
return new DropdownMenuItem<CheckoutMethod>(
198+
value: _parseStringToMethod(value),
209199
child: new Text(value),
210200
);
211201
}).toList(),
@@ -239,21 +229,17 @@ class _HomePageState extends State<HomePage> {
239229
);
240230
}
241231

242-
void _handleRadioValueChanged(int value) =>
243-
setState(() => _radioValue = value);
232+
void _handleRadioValueChanged(int? value) {
233+
if (value != null) setState(() => _radioValue = value);
234+
}
244235

245236
_handleCheckout(BuildContext context) async {
246-
if (_method == null) {
247-
_showMessage('Select checkout method first');
248-
return;
249-
}
250-
251237
if (_method != CheckoutMethod.card && _isLocal) {
252238
_showMessage('Select server initialization method at the top');
253239
return;
254240
}
255241
setState(() => _inProgress = true);
256-
_formKey.currentState.save();
242+
_formKey.currentState?.save();
257243
Charge charge = Charge()
258244
..amount = 10000 // In base currency
259245
..email = 'customer@email.com'
@@ -285,7 +271,7 @@ class _HomePageState extends State<HomePage> {
285271
}
286272

287273
_startAfreshCharge() async {
288-
_formKey.currentState.save();
274+
_formKey.currentState?.save();
289275

290276
Charge charge = Charge();
291277
charge.card = _getCardFromUI();
@@ -388,11 +374,8 @@ class _HomePageState extends State<HomePage> {
388374
),
389375
);
390376
} else {
391-
widget = new RaisedButton(
377+
widget = new ElevatedButton(
392378
onPressed: function,
393-
color: Colors.blueAccent,
394-
textColor: Colors.white,
395-
padding: const EdgeInsets.symmetric(vertical: 13.0, horizontal: 10.0),
396379
child: new Text(
397380
string.toUpperCase(),
398381
style: const TextStyle(fontSize: 17.0),
@@ -402,12 +385,12 @@ class _HomePageState extends State<HomePage> {
402385
return widget;
403386
}
404387

405-
Future<String> _fetchAccessCodeFrmServer(String reference) async {
388+
Future<String?> _fetchAccessCodeFrmServer(String reference) async {
406389
String url = '$backendUrl/new-access-code';
407-
String accessCode;
390+
String? accessCode;
408391
try {
409392
print("Access code url = $url");
410-
http.Response response = await http.get(url);
393+
http.Response response = await http.get(Uri.parse(url));
411394
accessCode = response.body;
412395
print('Response for access code = $accessCode');
413396
} catch (e) {
@@ -421,11 +404,11 @@ class _HomePageState extends State<HomePage> {
421404
return accessCode;
422405
}
423406

424-
void _verifyOnServer(String reference) async {
407+
void _verifyOnServer(String? reference) async {
425408
_updateStatus(reference, 'Verifying...');
426409
String url = '$backendUrl/verify/$reference';
427410
try {
428-
http.Response response = await http.get(url);
411+
http.Response response = await http.get(Uri.parse(url));
429412
var body = response.body;
430413
_updateStatus(reference, body);
431414
} catch (e) {
@@ -437,19 +420,16 @@ class _HomePageState extends State<HomePage> {
437420
setState(() => _inProgress = false);
438421
}
439422

440-
_updateStatus(String reference, String message) {
441-
_showMessage('Reference: $reference \n\ Response: $message',
442-
const Duration(seconds: 7));
423+
_updateStatus(String? reference, String message) {
424+
_showMessage('Reference: $reference \n\ Response: $message', const Duration(seconds: 7));
443425
}
444426

445-
_showMessage(String message,
446-
[Duration duration = const Duration(seconds: 4)]) {
447-
_scaffoldKey.currentState.showSnackBar(new SnackBar(
427+
_showMessage(String message, [Duration duration = const Duration(seconds: 4)]) {
428+
_scaffoldKey.currentState?.showSnackBar(new SnackBar(
448429
content: new Text(message),
449430
duration: duration,
450431
action: new SnackBarAction(
451-
label: 'CLOSE',
452-
onPressed: () => _scaffoldKey.currentState.removeCurrentSnackBar()),
432+
label: 'CLOSE', onPressed: () => _scaffoldKey.currentState?.removeCurrentSnackBar()),
453433
));
454434
}
455435

example/pubspec.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ dev_dependencies:
1414
path: ../
1515

1616
flutter:
17-
uses-material-design: true
17+
uses-material-design: true
18+
19+
environment:
20+
sdk: ">=2.12.0 <3.0.0"

example/test/widget_test.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ void main() {
1515
await tester.pumpWidget(new MyApp());
1616

1717
// Verify that platform version is retrieved.
18-
expect(
19-
find.byWidgetPredicate(
20-
(Widget widget) =>
21-
widget is Text && widget.data.startsWith('Running on:'),
22-
),
23-
findsOneWidget);
18+
expect(find.byWidgetPredicate(
19+
(Widget widget) {
20+
return widget is Text && widget.data?.startsWith('Running on:') == true;
21+
},
22+
), findsOneWidget);
2423
});
2524
}

lib/src/api/model/api_response.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class ApiResponse {
2-
String status;
3-
String message;
2+
String? status;
3+
String? message;
44
}

lib/src/api/model/transaction_api_response.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'package:flutter_paystack/src/api/model/api_response.dart';
22

33
class TransactionApiResponse extends ApiResponse {
4-
String reference;
5-
String trans;
6-
String auth;
7-
String otpMessage;
8-
String displayText;
4+
String? reference;
5+
String? trans;
6+
String? auth;
7+
String? otpMessage;
8+
String? displayText;
99

1010
TransactionApiResponse.unknownServerResponse() {
1111
status = '0';
@@ -27,11 +27,11 @@ class TransactionApiResponse extends ApiResponse {
2727
!map.containsKey('display_text') ? message : map['display_text'];
2828

2929
if (status != null) {
30-
status = status.toLowerCase();
30+
status = status!.toLowerCase();
3131
}
3232

3333
if (auth != null) {
34-
auth = auth.toLowerCase();
34+
auth = auth!.toLowerCase();
3535
}
3636
}
3737

@@ -47,11 +47,11 @@ class TransactionApiResponse extends ApiResponse {
4747
}
4848

4949
bool hasValidUrl() {
50-
if (otpMessage == null || otpMessage.length == 0) {
50+
if (otpMessage == null || otpMessage!.length == 0) {
5151
return false;
5252
}
5353

54-
return RegExp(r'^https?://', caseSensitive: false).hasMatch(otpMessage);
54+
return RegExp(r'^https?://', caseSensitive: false).hasMatch(otpMessage!);
5555
}
5656

5757
bool hasValidOtpMessage() {

lib/src/api/request/bank_charge_request_body.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ import 'package:flutter_paystack/src/models/charge.dart';
55
class BankChargeRequestBody extends BaseRequestBody {
66
String _accessCode;
77
BankAccount _account;
8-
String _birthday;
9-
String _token;
10-
String transactionId;
8+
String? _birthday;
9+
String? _token;
10+
String? transactionId;
1111

1212
BankChargeRequestBody(Charge charge)
13-
: this._accessCode = charge.accessCode,
14-
this._account = charge.account;
13+
: this._accessCode = charge.accessCode!,
14+
this._account = charge.account!;
1515

16-
Map<String, String> tokenParams() => {fieldDevice: device, 'token': _token};
16+
Map<String, String?> tokenParams() => {fieldDevice: device, 'token': _token};
1717

1818
@override
19-
Map<String, String> paramsMap() {
19+
Map<String, String?> paramsMap() {
2020
var map = {fieldDevice: device, 'account_number': account.number};
21-
if (_birthday != null) {
22-
map['birthday'] = _birthday;
23-
}
21+
map['birthday'] = _birthday;
2422
return map;
2523
}
2624

lib/src/api/request/base_request_body.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import 'package:flutter_paystack/src/common/platform_info.dart';
22

33
abstract class BaseRequestBody {
44
final fieldDevice = 'device';
5-
String _device;
5+
String? _device;
66

77
BaseRequestBody() {
88
_setDeviceId();
99
}
1010

11-
Map<String, String> paramsMap();
11+
Map<String, String?> paramsMap();
1212

13-
String get device => _device;
13+
String? get device => _device;
1414

1515
_setDeviceId() {
1616
String deviceId = PlatformInfo().deviceId ?? '';

0 commit comments

Comments
 (0)