Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a different dart file in Flutter?

I have created a dart file other than main.dart file in which i have created a grid view. So now I cannot understand how can I run this file of my project!

enter image description here

Above is the picture of my Android Studio where these flutter files are located. Please help me with running demo.dart file just like main.dart file of the project.

like image 696
hetsgandhi Avatar asked Jul 19 '18 10:07

hetsgandhi


People also ask

How do you use Dart in flutter?

The Dart installation comes with a VM as well to run the .dart files from a command-line interface. The Dart files used in Flutter apps are compiled and packaged into a binary file (.apk or .ipa) and uploaded to app stores. What does coding in Dart look like? The entry point of a Dart class is the main () method.

How to run a demo app from a DART file?

If you want demo.dart as the entry point of your app, you can right click on demo.dart and select Run 'demo.dart' (you need to define a main function in demo.dart to do so): If you just want to use what you defined in demo.dart from main.dart you have to add an import statement at the beginning of the main.dart file:

How to create a second DART file in VSCode?

Give a filename to it and the extension should be .dart. Step 2: Go to your main file where you want to refer to your second file. Step 3: Now start writing “ import ” on the top of your “ main.dart” file. VSCode should give you suggestions for the rest of the syntax. If otherwise, write “import ‘filename.dart’;”.

How to create a widget from a DART file?

The demo.dart file is also part of the project, therefore if you want to access it from your main.dart file, you will simply have to add the import file definition. Imagining the root widget you want to create is defined on the demo.dart, you can the use RunApp () function and attached the widget defined on it as the argument of this function.


2 Answers

Do flutter run with -t option and the file.dart.

$flutter run -t lib/demo.dart
like image 51
mykey Avatar answered Oct 05 '22 23:10

mykey


first set path to your Dart sdk (now included in flutter).

  1. if you create project using flutter and want to run separate single dart file let's say your file in lib/mydart/ then in terminal path to cd lib/mydart/ and dart yourfile.dart

yourfile.dart

void main() {
print('hello dart world');
}
  1. if you simply create .dart (hi.dart) file in any directory then in terminal write cd your_directory/ and run dart hi.dart

hi.dart

  void main() {
    print('hello dart world');
    }

you will see hello dart world output

like image 27
Mohsin AR Avatar answered Oct 05 '22 23:10

Mohsin AR