赞
踩
Flutter 开发中,常用的网络请求库为 Dio。本文对 Dio 进行简单封装。
base_res.dart
import 'package:dio/dio.dart'; class BaseResponse { // 通用参数,可根据实际业务修改 late int code; late String message; late dynamic data; // 业务请求是否成功 late bool success; // Dio 返回的原始 Response 数据 Response? ores; BaseResponse({ required this.code, required this.message, required this.data, required this.success, required this.ores, }); BaseResponse.fromJson(dynamic json) { code = json?['code'] ?? -1; message = json?['message'] ?? ''; data = json?['data'] ?? ''; success = code == 0 ? true : false; } }
api.dart
import 'package:flutter/foundation.dart';
class Api {
static const baseUrl = kDebugMode ? '' : '';
// login
static const loginUrl = '';
}
dio_intercept.dart
import 'package:dio/dio.dart';
class AuthInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// 此处根据业务逻辑,自行判断处理
if ('token' != '') {
options.headers[
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。