flutter: packages auto import problem in VS code

556 Views Asked by At

When I create a new Stateless or statefull class in any flutter app then VScode auto imports these three packages :

import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/container.dart'; 
import 'package:flutter/src/widgets/framework.dart';

So how can I configure VScode to import only the material package like this:

import 'package:flutter/material.dart';

I tried to change flutter and dart settings in the IDE but nothing changes.

1

There are 1 best solutions below

0
On
  1. Open VS Code and navigate to the "Preferences" menu (on Mac, this is under "Code"; on Windows, it's under "File").
  2. Select "User Snippets" from the dropdown menu.
  3. Select "dart.json" to open the Dart snippets file.
  4. Add the following code to the "stful" and "stless" snippets:
    "stful": {
        "prefix": "stful",
        "body": [
            "import 'package:flutter/material.dart';",
            "",
            "class ${1:MyWidget} extends StatefulWidget {",
            "const ${1:MyWidget}({super.key});",
            "",
            "  @override",
            "   State<${1:MyWidget}> createState() => _${1:MyWidget}State();",
            "}",
            "",
            "class _${1:MyWidget}State extends State<${1:MyWidget}> {",
            "  @override",
            "  Widget build(BuildContext context) {",
            "    return Scaffold(",
            "    );",
            "  }",
            "}"
        ],
        "description": "Stateful widget template"
    },
    "stless": {
        "prefix": "stless",
        "body": [
            "import 'package:flutter/material.dart';",
            "",
            "class ${1:MyWidget} extends StatelessWidget {",
            " const ${1:MyWidget}({super.key});",
            "",
            "  @override",
            "  Widget build(BuildContext context) {",
            "    return Scaffold(",
            "    );",
            "  }",
            "}"
        ],
        "description": "Stateless widget template"
    }
  1. Save the file. Now, every time you use the "stful" or "stless" snippets to create a new widget, the import 'package:flutter/material.dart'; statement will be included by default.