Home > Blockchain >  Flutter error: 2 Positional arguments expected, but 1 found
Flutter error: 2 Positional arguments expected, but 1 found

Time:11-03


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;
}


Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please Someone can help me with this? please

CodePudding user response:

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");

CodePudding user response:

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;
  • Related