Muhammad Ikhwan Perwira
Muhammad Ikhwan Perwira

Reputation: 1032

Import flutter library or dart library just once

I have main.dart file and home_page.dart file

home_page.dart:
______________________________________
import 'package:flutter/material.dart';
/* 
Using flutter lib...
*/
main.dart:
______________________________________
import 'package:flutter/material.dart';
import 'package:mypackage/home_page.dart'; // this library already using flutter material.dart
/* 
Using flutter lib...
*/

We can see, main.dart importing flutter library twice, I'm afraid it will waste storage and memory.

How to import package:flutter/material.dart once?

Because I want use that library/framework in repeteadly at separate files.

Upvotes: 0

Views: 1695

Answers (2)

Mostafa Mahmoud
Mostafa Mahmoud

Reputation: 671

First, don't worry about consuming memory.

On the other hand, you can create one file imports.dart, and use export

export 'package:flutter/material.dart';
export 'package:http/http.dart' as http;

and then import only this file

import 'imports.dart';

Upvotes: 1

h8moss
h8moss

Reputation: 5020

Importing a library twice will not use twice the memory!

When you add a library to your dependencies or dev dependencies (on the pubspec.yaml file) you download the dependency, this includes flutter, any subsequent imports only really make the dependency's API visible to the file.

Importing doesn't consume memory, depending does

When you import flutter you are saying. "Hello flutter, I would like to use some of the classes/variables you have defined on the material.dart file" both of your files ask this of flutter, but there is only one flutter.

Upvotes: 1

Related Questions