Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

android’s toast message in flutter

flutter application to show android’s toast message.

flutter_toast-message

methods and code to show toast message in flutter.

to show toast message in flutter, i’m communicating to native code using platfrom channels from flutter, and from native code i’m showing the toast-message.

platform channels:

which is provided by flutter to communicate between native code and flutter code.

define channel in flutter


    static const platform = const methodchannel('flutter.toast.message.channel');

define channel/handler in android


    methodchannel(flutterview, channel).setmethodcallhandler { call, result ->
          
    }

define platform channel as above and provide channel name,
this channel name must be same for both platform flutter code and android natice code.

create class in flutter:

to communicate with android in order to show toast-message.


    class toast {
      toast(string message) {
        _showtoast(message);
      }

      static const platform = const methodchannel('flutter.toast.message.channel');

      future<null> _showtoast(string message) async {
        // invoke method, provide method name and arguments.
        await platform.invokemethod('toast', {'message': message});
      }
    }

handle method invocation from flutter in android:


    class mainactivity : flutteractivity() {
        companion object {
            const val channel = "flutter.toast.message.channel"
            const val method_toast = "toast"
            const val key_message = "message"
        }

        override fun oncreate(savedinstancestate: bundle?) {
            super.oncreate(savedinstancestate)
            generatedpluginregistrant.registerwith(this)

            // handle method invocation from flutter, and perform action
            methodchannel(flutterview, channel).setmethodcallhandler { call, result ->
                if (call.method == method_toast) {
                    val message = call.argument<string>(key_message)
                    toast.maketext([email protected], message, toast.length_short).show()
                }
            }
        }
    }

that’s all we need to do to show android toast message in flutter.

usage:

to show toast messages from flutter, simply call toast class with your message as argument as below:


    toast("hello, i'm toast from flutter.");

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.