hetsgandhi
hetsgandhi

Reputation: 806

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.

Upvotes: 19

Views: 28003

Answers (8)

user20201944
user20201944

Reputation: 1

give import to the main.dart :-import 'Screens/demo.dart'; in main.dart , then in the material app there is a home section where you have to give the class name of the demo.dart instead of ProfileScreenWidget() just write class name function present in the demo.dart

After this ,just run from above

Upvotes: 0

Mohsin AR
Mohsin AR

Reputation: 3108

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

Upvotes: 3

Rahul Kushwaha
Rahul Kushwaha

Reputation: 6722

First create another dart file . As , I have created demo.dart file here.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Demo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      title: "Demo Application",
      home: new Scaffold(
        appBar: new AppBar(title: new Text("List Widget"),),
        body: new ListView(
          children: <Widget>[
            new ListTile(
              title: new Text("Apple"),
              trailing: new Icon(Icons.forward),
            ),
            new ListTile(
              title: new Text("Banana"),
              trailing: new Icon(Icons.backpack),
            ),
            new ListTile(
              title: new Text("Hultrdds"),
              trailing: new Icon(Icons.approval),
            )
          ],
        ),
      ),

    );
  }
}

And then in your main.dart file , import the demo.dart file and call like this.

import 'package:flutter/material.dart';
import 'demo.dart';

void main(){
  runApp(Demo());
}

Upvotes: 2

Mosfeq Anik
Mosfeq Anik

Reputation: 99

After creating a flutter project to run a different dart file in Flutter

  1. you have to add main function like below code with you desire class name

    void main() { runApp(newfile()); } or void main() => runApp(newfile());

  2. then go to project bar of android studio right click on the selected file you will see Run'newfile.dart' then you will also see in the toolbar, select your app from the run configurations drop-down menu

enter image description here

Upvotes: 2

mykey
mykey

Reputation: 2263

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

$flutter run -t lib/demo.dart

Upvotes: 18

Andrew
Andrew

Reputation: 37969

How run .dart file in terminal

1) Install Dart in your environment (if have not yet)

https://www.dartlang.org/tools/sdk#install

2) Add PATH variable for dart/bin

Example for Ubuntu

# add path example
echo 'export PATH="$PATH:/usr/lib/dart/bin"' >> ~/.bashrc
source .bashrc

3) And now just run your .dart file with the main() method in the terminal and see an output

$ dart path_to_your_file/your_file_with_main.dart

Upvotes: 6

Romain Rastel
Romain Rastel

Reputation: 5632

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):

enter image description here

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:

import 'demo.dart';

then you can use your GridView defined in demo.dart inside main.dart (if the GridView is not private).

Upvotes: 6

Sunil
Sunil

Reputation: 3494

from the left file explorer ->right click -> run

Upvotes: 0

Related Questions