Modeling of network requests, which manages input parameters and output parameters, which follows the "In-Out" naming convention.
- The prefix of the input parameter: in
- (inUsername, inPassword)
- The prefix of the return value: out
- (outLoginUser)
doAsync() async {
LoginAPI api = await LoginAPI(inNickname: 'jack', inPassword: '12345',).launch();
if (api.hasError) {
alert(api.outError);
} else {
User? currentUser = api.outUser;
if(currentUser != null) {
pagePush();
} else {
alert('User does not exist');
}
}
}
doSync() {
LoginAPI(inNickname: 'jack', inPassword: '12345').onComplete((api) {
if (api.hasError) {
alert(api.outError);
} else {
User? currentUser = api.outUser;
if (currentUser != null) {
pagePush();
} else {
alert('User does not exist');
}
}
}).launch();
}
class LoginAPI extends ModelAPI<LoginAPI> {
LoginAPI({required this.inNickname, required this.inPassword});
String inNickname;
String inPassword;
User? outUser;
@override
loading() async {
try {
outUser = await httpRequestUser();
} catch (e) {
outError = e;
} finally {
complete();
}
}
}