Want to use Functions of another File in Dart / Flutter? Another option, you can just declare all your functions (helpers) inside a class and pass them as an argument to other class.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //The class which contains your functions class HelperFunction{ //Define your method void launchWebView () { print("1234"); } //Pass that function to a class MyHomePage(launchWebView); } //The class which receives the function. class MyHomePage extends StatefulWidget{ //Retrieve the function and store it to a variable of type Function. final Function launchWebView; MyHomePage(this.launchWebView); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { //Access that function in State class using widget keyword. widget.launchWebView(); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.