Home > Software engineering >  How to run a Android Toast in a custom flutter plugin?
How to run a Android Toast in a custom flutter plugin?

Time:08-26

I am having some problems displaying a simple toast in a plugin I created in flutter. I will share some of my relevant code.

Here is my plugin class

public class somePlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  private TriPosDevice theDevice;
  private static final String CHANNEL1 = "com.example.something";
  private MethodChannel channel;
  private ActivityPluginBinding activityBinding;
  private FlutterPluginBinding flutterBinding;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    final VTP sharedVTP;
    sharedVTP = someSDK.getSharedVtp();
    flutterBinding = flutterPluginBinding;
    // This toast will fire why????
    Toast.makeText(getApplicationContext(), "THIS IS A TEST", Toast.LENGTH_LONG).show();    
    theDevice = new someDevice(sharedVTP, getActivity(), getApplicationContext());
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL1);
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    ...
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    activityBinding = binding;
  }

  @Override
  public void onDetachedFromActivity() {
    activityBinding = null;
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
    activityBinding = binding;
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    activityBinding = null;
  }
  
  public Context getApplicationContext() {
    return (flutterBinding != null) ? flutterBinding.getApplicationContext() : null;
  }

  public Activity getActivity() {
    return (activityBinding != null) ? activityBinding.getActivity() : null;
  }

}

Here is my second class that I am trying to call the toast in:

public class someDevice implements alot, of, things {
  final private VTP sharedVtp;
  final private Activity activity;
  final private Context context;

  someDevice(VTP sharedVtp, Activity activity, Context context) {
    this.sharedVtp = sharedVtp;
    this.activity = activity;
    this.context = context;
  }

   // This method is from one of the many things I implement
   // My Android is a little rusty but I believe this is two different ways of firing a toast
   // I am not seeing any toasts :(
   // I also know for a 100% fact that this method is being called. 
   @Override
    public void onConnected(Device device, String description, String model, String serialNumber) {
        Toast.makeText(context, String.format("Device connected!!!! description: %s model: %s serial Number: %s", description, model, serialNumber), Toast.LENGTH_LONG).show();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, String.format("Device connected!!!! description: %s model: %s serial Number: %s", description, model, serialNumber), Toast.LENGTH_LONG).show();
            }
        });
    }
}

Why do toasts work in the onAttachedToEngine method. Also, why are the toasts not working in my someDevice class and how can I fix it?

CodePudding user response:

Looks like I just needed a handler because there is no main activity xml file.

new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, String.format("Device connected!!!! description: %s model: %s serial Number: %s", description, model, serialNumber), Toast.LENGTH_LONG).show();
            }
        });

CodePudding user response:

you can try this library :- https://pub.dev/packages/fluttertoast the code to show the message :-

Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
  • Related