在 Visual Studio Code 中设置颤振

Posted

技术标签:

【中文标题】在 Visual Studio Code 中设置颤振【英文标题】:Setting up flutter in Visual Studio Code 【发布时间】:2021-09-18 01:54:01 【问题描述】:

您好,我最近从 (flutter.dev).com 下载了 Flutter 包 并提取它,然后在环境变量中设置flutter bin的路径:

并下载了所需的扩展并创建了一个新项目:

    import 'package:flutter/material.dart';

void main() 
  runApp(MyApp());


class MyApp extends StatelessWidget 
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  


class MyHomePage extends StatefulWidget 
  MyHomePage(Key? key, required this.title) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  int _counter = 0;

  void _incrementCounter() 
    setState(() 
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    );
  

  @override
  Widget build(BuildContext context) 
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text( 
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  

当我尝试在手机上运行它时,它给了我这个异常:

    FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Failed to install the following Android SDK packages as some licences have not been accepted.
     build-tools;29.0.2 Android SDK Build-Tools 29.0.2
  To build this project, accept the SDK license agreements and install the missing components using the Android Studio SDK Manager.
  Alternatively, to transfer the license agreements from one workstation to another, see http://d.android.com/r/studio-ui/export-licenses.html

  Using Android SDK: C:\Users\faroo\AppData\Local\Android\sdk

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 6s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

如果知道解决方案请帮帮我

【问题讨论】:

【参考方案1】:

您的问题是您没有接受 android 许可证。

在下方运行此命令并接受所有许可。

flutter doctor --android-licenses

然后重新启动 IDE 并重试。

祝你好运?

【讨论】:

好的,谢谢,我会尝试并告诉它是否有效?? FAILURE:构建失败并出现异常 * 出了什么问题 任务执行失败:app:compileDebugKotlin。 > Kotlin 在 Gradle 使用的 Java 安装“C:\Program Files (x86)\Java\jre1.8.0_291”中找不到所需的 JDK 工具。确保 Gradle 在 JDK 而不是 JRE 上运行 *尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。 * 在help.gradle.org BUILD FAILED in 9m 21s 获得更多帮助异常:Gradle 任务 assembleDebug 失败,退出代码为 1 退出(sigterm) 这是另一个问题,但会在 cmets 中回答,以免混淆上述答案。基本上,检查您的 PATH 并确保 JDK 在您的 PATH 中。快速谷歌如何添加路径到 PATH 环境变量应该可以解决问题。

以上是关于在 Visual Studio Code 中设置颤振的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Visual Studio Code 中设置用于调试 PowerShell 的环境变量?

在 Mac 上的 Visual Studio Code 调试器中设置环境变量

在 Visual Studio Code 中设置 .NET Core MVC Web 应用

在 Visual Studio Code 中设置 Cobol 制表符/缩进

在基于 Visual Studio Code Maven 的 Java 项目中设置类路径

如何在 Visual Studio Code 中设置我的工作区文件夹?