api
class WanAndroidApi {
/// 首页banner http://www.wanandroid.com/banner/json
static const String BANNER = “banner”;
static const String USER_REGISTER = “user/register”; //注册
static const String USER_LOGIN = “user/login”; //登录
static const String USER_LOGOUT = “user/logout”; //退出
// 拼接url
static String getPath({String path: ‘’, int page, String resType: ‘json’}) {
StringBuffer sb = new StringBuffer(path);
if (page != null) {
sb.write(’/$page’);
}
if (resType != null && resType.isNotEmpty) {
sb.write(’/$resType’);
}
return sb.toString();
}
}
请求与返回实体类 protocol
class LoginReq {
String username;
String password;
LoginReq(this.username, this.password);
-
LoginReq.fromJson(Map<String, dynamic> json)
- username = json[‘username’],
password = json[‘password’];
Map<String, dynamic> toJson() => {
‘username’: username,
‘password’: password,
};
@override
String toString() {
StringBuffer sb = new StringBuffer(’{’);
sb.write("“username”:"$username"");
sb.write(",“password”:$password");
sb.write(’}’);
return sb.toString();
}
}
接口请求&解析 repository
class WanRepository {
Future<L