電子商務(wù)網(wǎng)站開(kāi)發(fā)形式有友情鏈接出售網(wǎng)
問(wèn)題描述
問(wèn)題1:當(dāng)你使用FormData.from(Flutter3直接不能用)的時(shí)候,可能會(huì)提示沒(méi)有這個(gè)方法,或者使用FormData.fromMap(flutter3的dio支持)的時(shí)候也提示沒(méi)有,這時(shí)候可能就是和get庫(kù)里面的Formdata沖突了
問(wèn)題1:The method 'fromMap' isn't defined for the type 'FormData'. (Documentation) ?Try correcting the name to the name of an existing method, or defining a method named 'fromMap'
解決辦法:有可能是庫(kù)沖突了,因?yàn)間et和dio庫(kù)里面都有FormData導(dǎo)致的,使用別名更換一下
The method 'from' isn't defined for the type 'FormData'. (Documentation) ?Try correcting the name to the name of an existing method, or defining a method named 'from'?
解決辦法:更換使用 FormData.fromMap 這個(gè)方法 FormData.fromMap({})
然后你點(diǎn)擊進(jìn)去這個(gè)庫(kù)的源代碼看一下:發(fā)現(xiàn)確實(shí)沒(méi)有from或者fromMap方法,而且會(huì)默認(rèn)跳轉(zhuǎn)到get庫(kù)里面的FormData,怎么回事?我們應(yīng)該使用的是dio庫(kù)里面的啊
?這個(gè)時(shí)候就應(yīng)該意識(shí)到是庫(kù)沖突了,所以單獨(dú)引入dio庫(kù):
這個(gè)時(shí)候就會(huì)報(bào)另外一個(gè)問(wèn)題:
The name 'FormData' is defined in the libraries 'package:dio/src/form_data.dart (via package:dio/dio.dart)' and 'package:get/get_connect/http/src/multipart/form_data.dart'. (Documentation) ?Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.?
?
解決辦法
意思是:這個(gè)get庫(kù)和dio庫(kù)里面都有這個(gè)FormData,所以需要使用別名指定一下?
import 'package:dio/dio.dart' as dio_package;
然后使用這個(gè)前綴來(lái)引用:
?
Map<String, String> data = {"cardno": system.nfcId.toString(),"gender": system.gender.toString(),"username": system.nickName.toString(),"headpicurl": system.avatar.toString()};dio_package.FormData formData = dio_package.FormData.fromMap(data);
然后在使用dio發(fā)送請(qǐng)求的時(shí)候:直接使用post,然后把data放進(jìn)去就好了(因?yàn)閐io底層做了判斷是不是FormData,是的話,默認(rèn)會(huì)加上contentType)
var result = await Request().request("/v1/server/nfcadduser", method: DioMethod.post, data: data);// 返回?cái)?shù)據(jù)// print("getDetail:$result");return result;
dio底層做的判斷:?
然后服務(wù)器就可以接收到這個(gè)數(shù)據(jù)了:
封裝好的Dio對(duì)象
我這里封裝了一個(gè)Request對(duì)象:
import 'package:dio/dio.dart';
import 'package:flutter_windows/utils/storeage.dart';/// 請(qǐng)求方法:枚舉類型
enum DioMethod {get,post,put,delete,patch,head,
}// 創(chuàng)建請(qǐng)求類:封裝dio
class Request {/// 單例模式static Request? _instance;// 本地存儲(chǔ)對(duì)象final storage = Storage();// 工廠函數(shù):執(zhí)行初始化factory Request() => _instance ?? Request._internal();// 獲取實(shí)例對(duì)象時(shí),如果有實(shí)例對(duì)象就返回,沒(méi)有就初始化static Request? get instance => _instance ?? Request._internal();/// Dio實(shí)例static Dio _dio = Dio();// 初始化ip和端口String ip = "baseUrl地址";int port = 9080;/// 初始化Request._internal() {// 初始化基本選項(xiàng)BaseOptions options = BaseOptions(baseUrl: 'http://$ip:$port',connectTimeout: const Duration(seconds: 3),receiveTimeout: const Duration(seconds: 3));_instance = this;// 初始化dio_dio = Dio(options);// 添加攔截器_dio.interceptors.add(InterceptorsWrapper(onRequest: _onRequest, onResponse: _onResponse, onError: _onError));}// 重置ip地址void setIP(String ip) {_dio.options.baseUrl = ip;}/// 請(qǐng)求攔截器void _onRequest(RequestOptions options, RequestInterceptorHandler handler) {// 對(duì)非open的接口的請(qǐng)求參數(shù)全部增加userIdif (!options.path.contains("open")) {// options.queryParameters["userId"] = "xxx";}// 頭部添加token// options.headers["token"] = "xxx";// 更多業(yè)務(wù)需求handler.next(options);// super.onRequest(options, handler);}/// 相應(yīng)攔截器void _onResponse(Response response, ResponseInterceptorHandler handler) async {// 請(qǐng)求成功是對(duì)數(shù)據(jù)做基本處理if (response.statusCode == 200) {// 處理成功的響應(yīng)// print("響應(yīng)結(jié)果: $response");} else {// 處理異常結(jié)果print("響應(yīng)異常: $response");}handler.next(response);}/// 錯(cuò)誤處理: 網(wǎng)絡(luò)錯(cuò)誤等void _onError(DioException error, ErrorInterceptorHandler handler) {handler.next(error);}/// 請(qǐng)求類:支持異步請(qǐng)求操作Future<T> request<T>(String path, {DioMethod method = DioMethod.get,Map<String, dynamic>? params,dynamic data,CancelToken? cancelToken,Options? options,ProgressCallback? onSendProgress,ProgressCallback? onReceiveProgress,}) async {const methodValues = {DioMethod.get: 'get',DioMethod.post: 'post',DioMethod.put: 'put',DioMethod.delete: 'delete',DioMethod.patch: 'patch',DioMethod.head: 'head'};// 默認(rèn)配置選項(xiàng)options ??= Options(method: methodValues[method]);try {Response response;// 開(kāi)始發(fā)送請(qǐng)求response = await _dio.request(path,data: data,queryParameters: params,cancelToken: cancelToken,options: options,onSendProgress: onSendProgress,onReceiveProgress: onReceiveProgress);return response.data;} on DioException catch (e) {// print("發(fā)送請(qǐng)求異常: $e");rethrow;}}/// 開(kāi)啟日志打印/// 需要打印日志的接口在接口請(qǐng)求前 Request.instance?.openLog();void openLog() {_dio.interceptors.add(LogInterceptor(responseHeader: false, responseBody: true));}
}
?