Skip to content

Commit

Permalink
null safety migration
Browse files Browse the repository at this point in the history
Migrate for null safety.

Closes adaptant-labs#5.
  • Loading branch information
pmundt committed Mar 3, 2021
1 parent 18432e6 commit e739c20
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion example/vin_decoder_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void main() async {
print("Model year is " + vin.modelYear());
print("Serial number is " + vin.serialNumber());
print("Assembly plant is " + vin.assemblyPlant());
print("Manufacturer is " + vin.getManufacturer());
print("Manufacturer is " + vin.getManufacturer()!);
print("Year is " + vin.getYear().toString());
print("Region is " + vin.getRegion());
print("VIN string is " + vin.toString());
Expand Down
40 changes: 20 additions & 20 deletions lib/src/nhtsa_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'package:http/http.dart' as http;
import 'dart:convert';

class NHTSAResult {
String value;
String valueId;
String variable;
int variableId;
String? value;
String? valueId;
String? variable;
int? variableId;

NHTSAResult({this.value, this.valueId, this.variable, this.variableId});

Expand All @@ -24,10 +24,10 @@ class NHTSAResult {
}

class NHTSAVehicleInfo {
int count;
String message;
String searchCriteria;
List<NHTSAResult> results;
int? count;
String? message;
String? searchCriteria;
List<NHTSAResult>? results;

NHTSAVehicleInfo(
{this.count, this.message, this.searchCriteria, this.results});
Expand All @@ -37,10 +37,10 @@ class NHTSAVehicleInfo {
message = json['Message'];
searchCriteria = json['SearchCriteria'];
if (json['Results'] != null) {
results = List<NHTSAResult>();
results = [];
json['Results'].forEach((v) {
if (v['Value'] != null) {
results.add(NHTSAResult.fromJson(v));
results!.add(NHTSAResult.fromJson(v));
}
});
}
Expand All @@ -54,16 +54,16 @@ class NHTSAVehicleInfo {
ExtendedVehicleInfo toExtendedVehicleInfo() {
final ExtendedVehicleInfo info = ExtendedVehicleInfo();

results.forEach((f) {
results!.forEach((f) {
switch (f.variable) {
case "Vehicle Type":
info.vehicleType = normalizeStringValue(f.value);
info.vehicleType = normalizeStringValue(f.value!);
break;
case "Make":
info.make = normalizeStringValue(f.value);
info.make = normalizeStringValue(f.value!);
break;
case "Model":
info.model = normalizeStringValue(f.value);
info.model = normalizeStringValue(f.value!);
break;
}
});
Expand All @@ -78,15 +78,15 @@ class NHTSAVehicleInfo {
}

class ExtendedVehicleInfo {
String make;
String model;
String vehicleType;
String? make;
String? model;
String? vehicleType;

static Future<ExtendedVehicleInfo> getExtendedVehicleInfo(String vin) async {
var path = 'https://vpic.nhtsa.dot.gov/api//vehicles/DecodeVin/' +
static Future<ExtendedVehicleInfo?> getExtendedVehicleInfo(String vin) async {
var path = 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/' +
vin +
'?format=json';
final response = await http.get(path);
final response = await http.get(Uri.parse(path));

if (response.statusCode == 200) {
var vehicleInfo = NHTSAVehicleInfo.fromJson(jsonDecode(response.body));
Expand Down
18 changes: 9 additions & 9 deletions lib/src/vin_decoder_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class VIN {

/// Try to obtain extended information for the VIN from the NHTSA database.
final bool extended;
ExtendedVehicleInfo _info;
ExtendedVehicleInfo? _info;

VIN({@required this.number, this.extended = false})
VIN({required this.number, this.extended = false})
: wmi = normalize(number).substring(0, 3),
vds = normalize(number).substring(3, 9),
vis = normalize(number).substring(9, 17);

/// Carry out VIN validation. A valid [number] must be 17 characters long
/// and contain only valid alphanumeric characters.
bool valid([String number]) {
bool valid([String? number]) {
String value = normalize(number != null ? number : this.number);
return RegExp(r"^[a-zA-Z0-9]+$").hasMatch(value) && value.length == 17;
}
Expand All @@ -37,7 +37,7 @@ class VIN {
number.toUpperCase().replaceAll('-', '');

/// Obtain the encoded manufacturing year in YYYY format.
int getYear() {
int? getYear() {
return yearMap[modelYear()];
}

Expand Down Expand Up @@ -65,7 +65,7 @@ class VIN {
}

/// Get the full name of the vehicle manufacturer as defined by the [wmi].
String getManufacturer() {
String? getManufacturer() {
if (manufacturers.containsKey(this.wmi)) {
return manufacturers[this.wmi];
} else {
Expand All @@ -76,7 +76,7 @@ class VIN {
/// Returns the checksum for the VIN. Note that in the case of the EU region
/// checksums are not implemented, so this becomes a no-op. More information
/// is provided in ISO 3779:2009.
String getChecksum() {
String? getChecksum() {
return (getRegion() != "EU") ? normalize(this.number)[8] : null;
}

Expand All @@ -98,21 +98,21 @@ class VIN {

/// Get the Make of the vehicle from the NHTSA database if [extended] mode
/// is enabled.
Future<String> getMakeAsync() async {
Future<String?> getMakeAsync() async {
await _fetchExtendedVehicleInfo();
return this._info?.make;
}

/// Get the Model of the vehicle from the NHTSA database if [extended] mode
/// is enabled.
Future<String> getModelAsync() async {
Future<String?> getModelAsync() async {
await _fetchExtendedVehicleInfo();
return this._info?.model;
}

/// Get the Vehicle Type from the NHTSA database if [extended] mode is
/// enabled.
Future<String> getVehicleTypeAsync() async {
Future<String?> getVehicleTypeAsync() async {
await _fetchExtendedVehicleInfo();
return this._info?.vehicleType;
}
Expand Down
12 changes: 6 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ repository: https://github.com/adaptant-labs/vin-decoder-dart
issue_tracker: https://github.com/adaptant-labs/vin-decoder-dart/issues

environment:
sdk: '>=2.1.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
meta: ^1.2.4
basic_utils: ^2.6.3
http: ^0.12.2
random_string: ^2.1.0
meta: ^1.3.0
basic_utils: ^3.0.0-nullsafety.0
http: ^0.13.0
random_string: ^2.2.0-nullsafety

dev_dependencies:
test: ^1.15.7
test: ^1.16.5
4 changes: 2 additions & 2 deletions test/vin_decoder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:test/test.dart';

void main() {
group('EU VIN Test', () {
VIN vin;
late VIN vin;

setUp(() {
vin = VIN(number: 'WP0ZZZ99ZTS392124');
Expand All @@ -23,7 +23,7 @@ void main() {
});

group('AS VIN Test', () {
VIN vin;
late VIN vin;

setUp(() {
vin = VIN(number: 'JS1VX51L7X2175460');
Expand Down
2 changes: 1 addition & 1 deletion test/vin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:test/test.dart';
void main() {
group('VIN Generator Test', () {
VINGenerator generator;
VIN vin;
late VIN vin;

setUp(() {
generator = VINGenerator();
Expand Down

0 comments on commit e739c20

Please sign in to comment.