Flutter Unhandled Exception: DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The getter 'statusCode' was called on null

4,597

final Dio _dio = addInterceptors(Dio());

var res = await _dio.get(_endpoint+"/app-version");

Change to:

final Dio _dio = new Dio();

Response res = await _dio.get(_endpoint+"/app-version");

Share:
4,597
Arnau Bosch
Author by

Arnau Bosch

Updated on December 19, 2022

Comments

  • Arnau Bosch
    Arnau Bosch over 1 year

    In flutter I am trying to call and Endpoint that returns and object like this:

    {
      "latestVersion": "1.0.2",
      "minVersion": "1.0.1"
    }
    

    I am getting this error:

    I/flutter (17425): checkLatestVersion
    I/flutter (17425): provider
    I/flutter (17425): http://10.0.2.2:3000/api
    E/flutter (17425): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The getter 'statusCode' was called on null.
    E/flutter (17425): Receiver: null
    E/flutter (17425): Tried calling: statusCode
    E/flutter (17425): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
    E/flutter (17425): #1      errorInterceptor (package:kliik_app/dio/interceptor.dart:23:25)
    E/flutter (17425): #2      addInterceptors.<anonymous closure> (package:kliik_app/dio/interceptor.dart:19:39)
    E/flutter (17425): #3      InterceptorsWrapper.onError (package:dio/src/interceptor.dart:125:22)
    

    I don't understand why the call isn't performed. The problem is when I do the call:

    var res = await _dio.get(_endpoint+"/app-version");

    It seems like res is null or it doesn't do the get call, but I don't understand why.

    Thats part of my code:

    class _UpdateAppState extends State<UpdateApp> {
      final AppVersionApiProvider _provider = new AppVersionApiProvider();
    
      @override
      void initState() {
         
        super.initState();
    
        checkLatestVersion(context);
      }
    
      Future  checkLatestVersion(context) async {
        await Future.delayed(Duration(seconds: 5));
        print("checkLatestVersion");
        final AppVersionModel appVersion = await _provider.find();
        print("afterCallProvider");
        if (appVersion != null) {
    
          PackageInfo packageInfo = await PackageInfo.fromPlatform();
          Version currentVersion = Version.parse(packageInfo.version);
    
          if (appVersion.minVersion > currentVersion) {
            _showCompulsoryUpdateDialog(
              context,
              "Siusplau, actualitza l'app per continuar.",
            );
          } else if (appVersion.latestVersion > currentVersion) {
            SharedPreferences sharedPreferences =
                await SharedPreferences.getInstance();
    
            bool showUpdates = false;
            showUpdates = sharedPreferences.getBool(kUpdateDialogKeyName);
            if (showUpdates != null && showUpdates == false) {
              return;
            }
    
            _showOptionalUpdateDialog(
              context,
              "Hi ha una nova versió de l'app disponible. Vols actualitzar-la?",
            );
            print('Update available');
          } else {
            print('App is up to date');
          }
        }
      }
    

    Provider class: await _provider.find()

    class AppVersionApiProvider {
      final String _endpoint = conf.api;
      final Dio _dio = addInterceptors(Dio());
    
      Future<AppVersionModel> find() async {
         print("provider");
         print(_endpoint);
         var res = await _dio.get(_endpoint+"/app-version");
        
        if(res.statusCode == 200) {
          print("test");
          return AppVersionModel.fromJson(res.data);
        }
        return null;
      }
    }
    

    Backend API:

    import { Controller, Get } from '@nestjs/common';
    import { AppVersionService } from './app-version.service';
    
    @Controller('app-version')
    export class AppVersionController {
    
      constructor(
        private appVersionService: AppVersionService
      ) { }
    
      @Get('/')
      async appVersion() {
        console.log("app-versiooooon");
        return this.appVersionService.get();
      }
    }
    

    The API works fine, I open the Google Chrome and I go to:

    http://localhost:3000/api/app-version

    It returns:

    // 20201123095650
    // http://localhost:3000/api/app-version
    ​
    {
      "latestVersion": "1.0.2",
      "minVersion": "1.0.1"
    }
    
  • Arnau Bosch
    Arnau Bosch over 3 years
    @Vinah H P I have tried what you said: I am getting the same error: [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The getter 'statusCode' was called on null. E/flutter (28532): Receiver: null E/flutter (28532): Tried calling: statusCode Also it says <asynchronous suspension>
  • Arnau Bosch
    Arnau Bosch over 3 years
    @Vinah H P Hi, It doesn't print nothing. I think that the problem is with the call await _dio.get(_endpoint+"/app-version");
  • Admin
    Admin over 3 years
    Response res = await _dio.get(_endpoint+"/app-version"); print(res.statusCode); Tell me what you get..
  • Arnau Bosch
    Arnau Bosch over 3 years
    Vinay H P this: [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The getter 'statusCode' was called on null. E/flutter (30618): Receiver: null E/flutter (30618): Tried calling: statusCode
  • Admin
    Admin over 3 years
    @Arnau final Dio _dio = new Dio(); Try without addInterceptors(Dio()); Lets debug one step at a time. I assume you are using Android Studio, This Package pub.dev/packages/dio.. and Android Studio Emulator.
  • Admin
    Admin over 3 years
    @Arnau <asynchronous suspension> is not an indication of a problem, it just indicates that code execution is not synchronous code executed line-by-line, but instead a callback from a completed Future started at some position in the callstack.
  • Admin
    Admin over 3 years
    @ArnauBoschTekla.io medium.com/flutter-community/…
  • Arnau Bosch
    Arnau Bosch over 3 years
    yeah it was that. I have remove addIntercepetors(Dio()) and now it works! I dont understand why but thank you
  • Admin
    Admin over 3 years
    Hi @Arnau Bosch, Just a request. I need reputation points so if you could vote and select this answer as accepted answer, I will get some. I am new, trying to answer the questions and help others. So, please help me by doing that.