Skip to content

Commit e1c9c62

Browse files
authored
Merge pull request #126 from lohanidamodar/feat-dart-sdk-null-safety
Dart sdk null safety migration
2 parents d875e69 + 4371268 commit e1c9c62

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

templates/dart/lib/client.dart.twig

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ part of {{ language.params.packageName }};
33
class Client {
44
String endPoint;
55
String type = 'unknown';
6-
Map<String, String> headers;
7-
Map<String, String> config;
6+
Map<String, String>? headers;
7+
late Map<String, String> config;
88
bool selfSigned;
99
bool initialized = false;
1010
Dio http;
1111

12-
Client({this.endPoint = '{{spec.endpoint}}', this.selfSigned = false, Dio http}) : this.http = http ?? Dio() {
12+
Client({this.endPoint = '{{spec.endpoint}}', this.selfSigned = false, Dio? http}) : this.http = http ?? Dio() {
1313

1414
this.headers = {
1515
'content-type': 'application/json',
@@ -48,19 +48,19 @@ class Client {
4848
}
4949

5050
Client addHeader(String key, String value) {
51-
headers[key] = value;
51+
headers![key] = value;
5252

5353
return this;
5454
}
5555

5656
Future init() async {
5757
if(!initialized) {
5858
this.http.options.baseUrl = this.endPoint;
59-
this.http.options.validateStatus = (status) => status < 400;
59+
this.http.options.validateStatus = (status) => status! < 400;
6060
}
6161
}
6262

63-
Future<Response> call(HttpMethod method, {String path = '', Map<String, String> headers = const {}, Map<String, dynamic> params = const {}, ResponseType responseType}) async {
63+
Future<Response> call(HttpMethod method, {String path = '', Map<String, String> headers = const {}, Map<String, dynamic> params = const {}, ResponseType? responseType}) async {
6464
if(selfSigned) {
6565
// Allow self signed requests
6666
(http.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
@@ -73,14 +73,15 @@ class Client {
7373

7474
// Origin is hardcoded for testing
7575
Options options = Options(
76-
headers: {...this.headers, ...headers},
76+
headers: {...this.headers!, ...headers},
7777
method: method.name(),
78-
responseType: responseType
78+
responseType: responseType,
79+
listFormat: ListFormat.multiCompatible,
7980
);
8081
try {
8182

8283
if(headers['content-type'] == 'multipart/form-data') {
83-
return await http.request(path, data: FormData.fromMap(params), options: options);
84+
return await http.request(path, data: FormData.fromMap(params,ListFormat.multiCompatible), options: options);
8485
}
8586

8687
if (method == HttpMethod.get) {
@@ -97,16 +98,16 @@ class Client {
9798
throw {{spec.title | caseUcfirst}}Exception(e.message);
9899
}
99100
if(responseType == ResponseType.bytes) {
100-
if(e.response.headers['content-type'].contains('application/json')) {
101-
final res = json.decode(utf8.decode(e.response.data));
102-
throw {{spec.title | caseUcfirst}}Exception(res['message'],res['code'], e.response);
101+
if(e.response!.headers['content-type']?.contains('application/json') ?? false) {
102+
final res = json.decode(utf8.decode(e.response!.data));
103+
throw {{spec.title | caseUcfirst}}Exception(res['message'],res['code'], res);
103104
} else {
104105
throw {{spec.title | caseUcfirst}}Exception(e.message);
105106
}
106107
}
107-
throw {{spec.title | caseUcfirst}}Exception(e.response.data['message'],e.response.data['code'], e.response.data);
108+
throw {{spec.title | caseUcfirst}}Exception(e.response?.data['message'],e.response?.data['code'], e.response?.data);
108109
} catch(e) {
109-
throw {{spec.title | caseUcfirst}}Exception(e.message);
110+
throw {{spec.title | caseUcfirst}}Exception(e.toString());
110111
}
111112
}
112113
}

templates/dart/lib/exception.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of {{ language.params.packageName }};
22

33
class {{spec.title | caseUcfirst}}Exception implements Exception {
4-
final String message;
5-
final int code;
4+
final String? message;
5+
final int? code;
66
final dynamic response;
77

88
{{spec.title | caseUcfirst}}Exception([this.message = "", this.code, this.response]);

templates/dart/lib/services/service.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of {{ language.params.packageName }};
22

33
{% macro parameter(parameter) %}
4-
{% if parameter.name == 'orderType' %}{% if parameter.required %}@required {% endif %}{{ 'OrderType orderType = OrderType.asc' }}{% else %}
5-
{% if parameter.required %}@required {% endif %}{{ parameter.type | typeName }} {{ parameter.name | caseCamel }}{{ parameter | escapeDollarSign | paramDefault }}{% endif %}
4+
{% if parameter.name == 'orderType' %}{% if parameter.required %}required {% endif %}{{ 'OrderType orderType = OrderType.asc' }}{% else %}
5+
{% if parameter.required %}required {% endif %}{{ parameter.type | typeName }} {{ parameter.name | caseCamel }}{{ parameter | escapeDollarSign | paramDefault }}{% endif %}
66
{% endmacro %}
77
{% macro method_parameters(parameters) %}
88
{% if parameters.all|length > 0 %}{{ '{' }}{% for parameter in parameters.all %}{{ _self.parameter(parameter) }}{% if not loop.last %}, {% endif %}{% endfor %}{{ '}' }}{% endif %}

templates/dart/pubspec.yaml.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ repository: https://github.com/{{sdk.gitUserName}}/{{sdk.gitRepoName}}
66
issue_tracker: https://github.com/appwrite/sdk-generator/issues
77
documentation: {{ spec.contactURL }}
88
environment:
9-
sdk: '>=2.6.0 <3.0.0'
9+
sdk: '>=2.12.0 <3.0.0'
1010
dependencies:
11-
dio: ^3.0.10
12-
meta: ^1.1.8
11+
dio: ^4.0.0-prev3
12+
meta: ^1.3.0
1313
1414
dev_dependencies:
15-
test:
15+
test: ^1.16.5

tests/SDKTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,11 @@ class SDKTest extends TestCase
5151
'build' => [
5252
'mkdir -p tests/sdks/dart/tests',
5353
'cp tests/languages/dart/tests.dart tests/sdks/dart/tests/tests.dart',
54-
'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.7 pub get',
54+
'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.12 pub get',
5555
],
5656
'envs' => [
57-
// 'dart-2.6' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.6 pub run tests/tests.dart',
58-
'dart-2.7' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.7 pub run tests/tests.dart',
59-
'dart-2.8' => 'docker run --rm --tty -it -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.8 pub run tests/tests.dart',
60-
'dart-2.10' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.10 dart pub run tests/tests.dart',
61-
'dart-2.12-beta' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.12-beta dart pub run tests/tests.dart',
57+
'dart-2.12' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.12 dart pub run tests/tests.dart',
58+
'dart-2.13-dev' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart --env PUB_CACHE=vendor google/dart:2.13-dev dart pub run tests/tests.dart',
6259
],
6360
'supportException' => true,
6461
],

0 commit comments

Comments
 (0)