请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

The getter 'data' was called on null.

图片描述

import 'package:bibilili/http/core/hi_adapter.dart';
import 'package:bibilili/http/core/hi_error.dart';
import 'package:bibilili/http/core/mock_adapter.dart';
import 'package:bibilili/http/request/base_request.dart';


class HiNet{
  HiNet._();
  static HiNet _instance;
  static HiNet getInstance(){
    _instance= HiNet._();
    return _instance;
  }
  Future fire(BaseRequest request) async {
   HiNetResponse response;
    var error;
    try{
      response = await send(request);
    } on HiNetError catch(e){
      error = e;
      response=e.data;
      printLog(e.message);
    } catch(e){
      //其他异常
      error = e;
      printLog(e);
    }

    if(response==null){
      printLog(error);
    }

    var result = response.data;
    printLog(result);

    var status = response.statusCode;
    switch(status){
      case 200:
        return result;
      case 401:
        throw NeedLogin();
      case 403:
        throw NeedAuth(request.toString(),data: result);
      default:
        throw HiNetError(status, result.toString(),data:result);
    }
  }

  Future<dynamic> send<T>(BaseRequest request) async {
    printLog("url:${request.url()}");
    ///使用Mock发送请求
    HiNetAdapter adapter=MockAdapter();
    return adapter.send(request);
  }
  void printLog(log){
    print("hi_net:${log.toString()}");
  }
}

正在回答 回答被采纳积分+3

1回答

CrazyCodeBoy 2021-12-27 09:06:28

第30当response为空的时候加个 return
或者做下空安全适配,可参考下这块的实现:https://git.imooc.com/coding-487/fa-net/src/master/lib/http/core/hi_net.dart#L24

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信