3

I am trying to call an argument from my Java native code to Dart, I have a method in my Java code -

@Override
public void onReward(final int quantity) {
    HashMap<String, Object> args = new HashMap<>();
    args.put("reward",quantity);
    Log.i("TheoremReach", "onRewardCenterOpened");
    TrsurveysPlugin.getInstance().OnMethodCallHandler("onReward", args);
}

I am storing my args in my HashMap and passing it as an argument using my OnMethodCallHandler

void OnMethodCallHandler(final String method, final HashMap<String,Object> args) {
    try {
        TrsurveysPlugin.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                channel.invokeMethod(method, args);
            }
        });
     } catch (Exception e) {
         Log.e("Trsurveys", "Error " + e.toString());
     }
 }

How do I call this in my Dart code? Here's my current attempt -

This is how I get a callback from the Java side of code, however this doesn't return my int quantity from the method -

typedef TheoremListener(TheoremReachListener listener);

static final Map<String, TheoremReachListener> theoremReachListener = {
    'onReward': TheoremReachListener.onReward,
};

static Future<void> handleMethod(
    MethodCall call, TheoremListener listener) async {
    listener(theoremReachListener[call.method]);
}

Here is my listener from my example repo, it prints the onReward but doesn't return my argument

listener(TheoremReachListener event) {
    if (event == TheoremReachListener.onReward) {
        print('on reward');
    } 
}
Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
Arnav
  • 1,404
  • 2
  • 19
  • 38

0 Answers0