Home > Enterprise >  Import flutter library or dart library just once
Import flutter library or dart library just once

Time:12-23

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.

CodePudding user response:

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.

CodePudding user response:

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';
  • Related