MissingPluginException(No implementation found for method read on channel plugins.it_nomads.com/flutter_secure_storage)
Asked Answered
T

4

7

I'm trying to write some unit tests for ApiBaseHelper methods (responsible for all HTTP requests).

class ApiBaseHelper {
  String _baseUrl = "${Config.get('apiUrl')}/";
  Dio _dio = Dio()..interceptors.add(DioCustomInterceptors());

  ApiBaseHelper({Dio? dio}) {
    _dio = dio ?? Dio()
      ..interceptors.add(DioCustomInterceptors());
    _dio.options.baseUrl = _baseUrl;
  }

  Future<dynamic> get(String url, [Map<String, dynamic>? query]) async {
    try {
      Response<String> response =
          await _dio.get(_baseUrl + url, queryParameters: query);
      if (response.statusCode == 200) {
        return response.data;
      }
    } on DioError catch (e) {
      _returnException(e.response);
    }
  }
}

this is the file where I tried to test the GET method, I use the Dio package

import 'package:app/utils/services/network/api_base_helper.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';

const successMessage = {'message': 'Success'};
const errorMessage = {'message': 'error'};
const testPath = 'test';
const testData = {'data': 'sample data'};
const header = {'Content-Type': 'application/json'};

void main(){
  WidgetsFlutterBinding.ensureInitialized() ;
  final dio = Dio();
  final dioAdapter = DioAdapter(dio: dio);
  var baseUrl;


  setUp(() {
    dio.httpClientAdapter = dioAdapter;
    baseUrl = 'https://example.com/';
  });

  group("", (){
    test('Test Get', () async {
      dioAdapter.onGet(
        '$baseUrl$testPath',
            (request) {
          return request.reply(200, successMessage);
        },
        data: null,
        queryParameters: {},
        headers: {},
      );
      final service = ApiBaseHelper(
        dio: dio,
      );
      final response = await service.get('test');
      expect(response, successMessage);
    });
  });
}

when I run test I got this erro

package:flutter/src/services/platform_channel.dart 175:7       MethodChannel._invokeMethod
===== asynchronous gap ===========================
dart:async                                                     _asyncErrorWrapperHelper
package:dio/src/dio_mixin.dart 502:28                          DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>.<fn>
package:dio/src/dio_mixin.dart 818:22                          DioMixin.checkIfNeedEnqueue
package:dio/src/dio_mixin.dart 500:22                          DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>
===== asynchronous gap ===========================
dart:async                                                     new Future
package:dio/src/dio_mixin.dart 499:13                          DioMixin.fetch._requestInterceptorWrapper.<fn>
package:dio/src/dio_mixin.dart 494:14                          DioMixin.fetch._requestInterceptorWrapper.<fn>
===== asynchronous gap ===========================
dart:async                                                     Future.then
package:dio/src/dio_mixin.dart 581:23                          DioMixin.fetch.<fn>
dart:collection                                                ListMixin.forEach
package:dio/src/dio_mixin.dart 577:18                          DioMixin.fetch
package:dio/src/dio_mixin.dart 468:12                          DioMixin.request
package:dio/src/dio_mixin.dart 55:12                           DioMixin.get
package:app/utils/services/network/api_base_helper.dart 19:22  ApiBaseHelper.get
test/network/api_base_helper_test.dart 40:38                   main.<fn>.<fn>
test/network/api_base_helper_test.dart 27:22                   main.<fn>.<fn>

MissingPluginException(No implementation found for method read on channel plugins.it_nomads.com/flutter_secure_storage)

I'm using flutter secure storage to store user credential, but here I'm just trying to test ApiBaseHelper. any help please?

Teacart answered 14/4, 2022 at 14:19 Comment(0)
H
10

I had the exact same error when I was trying to test a different method provided by Flutter_Secure_Storage.

I was able to fix the issue by writing the following in setUpAll function:

setUpAll(() {
  FlutterSecureStorage.setMockInitialValues({});
});

More info here: Flutter Test MissingPluginException

Hope this helps.

Hampstead answered 9/12, 2022 at 3:29 Comment(0)
K
4

Stop the application debug and relaunch it. This must resolve the issue.

Kaceykachina answered 15/4, 2022 at 9:28 Comment(2)
This solved the issue when debug on chrome.Athematic
And the github issue mentioned in the question is not solved yet. Those the issue is closed and don't accept any comment.Athematic
F
0

I had the same error when my integration_test was under /test/integration_test

Try placing your test folder code directly in project root folders such as /test or /integration_test (at same level as /lib)

Frenchify answered 26/7, 2022 at 10:15 Comment(0)
S
0

I had this problem twice and the solution was always to delay the reading. First you have to be assured that everything is builded. Then you call your _load() method. This last time I builded everything and let to the user to tap on the control to call the _load() method. I did this way to avoid blank screen for long time, but I'm sure there are other ways to apply this idea.

Slenderize answered 4/10 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.