Skip to content

Commit b47195f

Browse files
committed
2.0
1 parent d67330e commit b47195f

File tree

9 files changed

+240
-214
lines changed

9 files changed

+240
-214
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
## 1.0.0
2+
* Initial release
23

3-
* TODO: Describe initial release.
4+
## 2.0.0
5+
* Updated naming conventions

README.md

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,75 @@
88
- (outLoginUser)
99

1010
## Getting started
11-
12-
13-
## Usage
11+
#### Installing
12+
```yaml
13+
dependencies:
14+
flutter_api_model: latest_version
15+
```
16+
#### Importing
1417
```dart
15-
doAsync() async {
16-
LoginAPI api = await LoginAPI(inNickname: 'jack', inPassword: '12345',).execute();
17-
if (api.hasError) {
18-
alert(api.outError);
19-
} else {
20-
User? currentUser = api.outUser;
21-
if(currentUser != null) {
22-
pagePush();
23-
} else {
24-
alert('User does not exist');
25-
}
26-
}
27-
}
18+
import 'package:flutter_api_model/flutter_api_model.dart';
2819
```
2920

21+
### Using `await`
3022
```dart
31-
doSync() {
32-
LoginAPI(inNickname: 'jack', inPassword: '12345').onComplete((api) {
33-
if (api.hasError) {
34-
alert(api.outError);
35-
} else {
36-
User? currentUser = api.outUser;
37-
if (currentUser != null) {
38-
pagePush();
39-
} else {
40-
alert('User does not exist');
41-
}
42-
}
43-
}).execute();
23+
final model = await ProfileAPIModel(inUserId: '2024').start();
24+
if (model.hasError) {
25+
final error = model.outError;
26+
} else {
27+
final user = model.outUser;
4428
}
4529
```
46-
### class define
30+
31+
### Using callback
4732
```dart
48-
class LoginAPI extends ModelAPI<LoginAPI> {
49-
LoginAPI({required this.inNickname, required this.inPassword});
33+
ProfileAPIModel(inUserId: '2024').onComplete((model) {
34+
if (!model.hasError) {
35+
final user = model.outUser;
36+
} else {
37+
final error = model.outError;
38+
}
39+
}).start();
5040
51-
String inNickname;
41+
```
5242

53-
String inPassword;
43+
### Class definition
44+
```dart
45+
class ProfileAPIModel extends BaseAPI with model<ProfileAPIModel>, APIWithLoginNeed {
46+
ProfileAPIModel({required this.inUserId});
5447
48+
/// Input parameter
49+
String inUserId;
50+
/// Output result
5551
User? outUser;
5652
5753
@override
58-
loading() async {
54+
load() async {
5955
try {
60-
outUser = await httpRequestUser();
56+
final response = await dio.request('/user/profile');
57+
outUser = User.jsonToUser(response.data['user']);
6158
} catch (e) {
6259
outError = e;
6360
} finally {
64-
complete();
61+
finalize();
6562
}
6663
}
6764
}
68-
```
6965
70-
## Additional information
66+
/// Defines a base type if initialization work is needed
67+
class BaseAPI {
68+
Dio dio = Dio();
69+
70+
BaseAPI() {
71+
dio.options.baseUrl = 'https://base_url.com';
72+
dio.options.headers = {'token': '2024'};
73+
}
74+
}
75+
76+
mixin APIWithLoginNeed {
77+
bool hasPermission() {
78+
return isLogin();
79+
}
80+
}
7181
82+
```

example/example.md

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,75 @@
88
- (outLoginUser)
99

1010
## Getting started
11+
#### Installing
12+
```yaml
13+
dependencies:
14+
flutter_api_model: latest_version
15+
```
16+
#### Importing
17+
```dart
18+
import 'package:flutter_api_model/flutter_api_model.dart';
19+
```
1120

12-
13-
## Usage
14-
### await形式
21+
### Using `await`
1522
```dart
16-
final requestModel = await ProfileRequestModel(inUserId: '2024').execute();
17-
if (requestModel.hasError) {
18-
final error = requestModel.outError;
23+
final model = await ProfileAPIModel(inUserId: '2024').start();
24+
if (model.hasError) {
25+
final error = model.outError;
1926
} else {
20-
final user = requestModel.outUser;
27+
final user = model.outUser;
2128
}
2229
```
23-
### 回调形式
30+
31+
### Using callback
2432
```dart
25-
ProfileRequestModel(inUserId: '2024').onComplete((model) {
26-
if (!requestModel.hasError) {
27-
final user = requestModel.outUser;
28-
} else {
29-
final error = requestModel.outError;
30-
}
31-
}).execute();
33+
ProfileAPIModel(inUserId: '2024').onComplete((model) {
34+
if (!model.hasError) {
35+
final user = model.outUser;
36+
} else {
37+
final error = model.outError;
38+
}
39+
}).start();
40+
3241
```
33-
### Class define
42+
43+
### Class definition
3444
```dart
35-
class ProfileRequestModel extends BaseRequest
36-
with RequestModel<ProfileRequestModel>, RquestModelWithLoginNeed {
37-
ProfileRequestModel({required this.inUserId});
45+
class ProfileAPIModel extends BaseAPI with model<ProfileAPIModel>, APIWithLoginNeed {
46+
ProfileAPIModel({required this.inUserId});
3847
48+
/// Input parameter
3949
String inUserId;
40-
50+
/// Output result
4151
User? outUser;
4252
4353
@override
44-
loading() async {
54+
load() async {
4555
try {
4656
final response = await dio.request('/user/profile');
4757
outUser = User.jsonToUser(response.data['user']);
4858
} catch (e) {
4959
outError = e;
5060
} finally {
51-
complete();
61+
finalize();
5262
}
5363
}
5464
}
5565
56-
class BaseRequest {
66+
/// Defines a base type if initialization work is needed
67+
class BaseAPI {
5768
Dio dio = Dio();
5869
59-
BaseRequest() {
70+
BaseAPI() {
6071
dio.options.baseUrl = 'https://base_url.com';
6172
dio.options.headers = {'token': '2024'};
6273
}
6374
}
6475
65-
mixin RquestModelWithLoginNeed {
76+
mixin APIWithLoginNeed {
6677
bool hasPermission() {
6778
return isLogin();
6879
}
6980
}
70-
```
71-
72-
## Additional information
7381
82+
```

lib/flutter_api_model.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library flutter_request_model;
2+
3+
export 'src/api_model.dart';
4+
export 'src/api_model_on_list.dart';

0 commit comments

Comments
 (0)