0

I receive the above error message in the following code.

import 'package:flutter/services.dart';
import 'dart:io'; // for File
import 'package:file_picker/file_picker.dart'; // for FilePickerResultfile_picker: ^5.2.2

typedef OnRewardListener = void Function(num quantity, String? uid);
typedef SurveyAvailableListener = void Function(int? survey);
typedef RewardCenterOpenedListener = void Function();
typedef RewardCenterClosedListener = void Function();

class RapidoReach {
  static RapidoReach get instance => _instance;

  final MethodChannel _channel;

  static final RapidoReach _instance = RapidoReach.private(
    const MethodChannel('rapidoreach'),
  );

  RapidoReach.private(MethodChannel channel) : _channel = channel {
    _channel.setMethodCallHandler(_platformCallHandler);
  }

  static OnRewardListener? _onRewardListener;
  static SurveyAvailableListener? _surveyAvailableListener;
  static RewardCenterOpenedListener? _rewardCenterOpenedListener;
  static RewardCenterClosedListener? _rewardCenterClosedListener;

  Future<void> init({String? apiToken, String? userId}) async {
    assert(apiToken != null && apiToken.isNotEmpty);
    return _channel.invokeMethod(
        "init", <String, dynamic>{"api_token": apiToken, "user_id": userId});
  }

  Future<void> show({String? placementID}) {
    return _channel
        .invokeMethod("show", <String, dynamic>{"placementID": placementID});
  }


  Future _platformCallHandler(MethodCall call) async {
    debugPrint(
        "RapidoReach _platformCallHandler call ${call.method} ${call.arguments}");

    switch (call.method) {
      case "onReward":
        _onRewardListener!(call.arguments); //Here is the error, in call.arguments
        break;

      case "rapidoReachSurveyAvailable":
        _surveyAvailableListener!(call.arguments);
        break;

      case "onRewardCenterOpened":
        _rewardCenterOpenedListener!();
        break;

      case "onRewardCenterClosed":
        _rewardCenterClosedListener!();
        break;
      default:
        debugPrint('Unknown method ${call.method}');
    }
  }

  void setOnRewardListener(OnRewardListener? onRewardListener) =>
      _onRewardListener = onRewardListener;

  void setSurveyAvaiableListener(
          SurveyAvailableListener? surveyAvailableListener) =>
      _surveyAvailableListener = surveyAvailableListener;

  void setRewardCenterOpened(
          RewardCenterOpenedListener? rewardCenterOpenedListener) =>
      _rewardCenterOpenedListener = rewardCenterOpenedListener;

  void setRewardCenterClosed(
          RewardCenterClosedListener? rewardCenterClosedListener) =>
      _rewardCenterClosedListener = rewardCenterClosedListener;
}

What can I do about it?

jraufeisen
  • 3,005
  • 7
  • 27
  • 43
  • `call.arguments` might be a map or a list, depending on your native code which you don't show. (You print it a few lines before, so you should be able to see what it contains.) Perhaps you meant to access the two values out of it - assuming it's a map. – Richard Heap Nov 02 '22 at 23:36

2 Answers2

0

At the top of your file you declare

typedef OnRewardListener = void Function(num quantity, String? uid);

Which means that a function of type OnRewardListener will need two arguments: One for the quantity and one for the uid.

Later in your code you call

_onRewardListener!(call.arguments);

This is the source of the problem: call.arguments is only one parameter. But you would have to call it with two values, e.g. via

_onRewardListener!(5, "myUID");
jraufeisen
  • 3,005
  • 7
  • 27
  • 43
-1

Add This Package in your project :

dependencies:
  uuid: ^3.0.6 

then :

import 'package:uuid/uuid.dart';

var uuid = Uuid();


switch (call.method) {
      case "onReward":
        _onRewardListener!(call.arguments ,uuid.v4().toString() );  // Add this Changes here 
        break;
  • Why pass a UUID? There's absolutely no indication that's what the `uid` parameter is for. Unlike the earlier answer, this does not explain the actual problem, it just generates a random string and passes it in place of the missing parameter. – Ryan M Sep 01 '23 at 01:52