Flutter - 无法在 macOS 上启动应用程序进行调试
Posted
技术标签:
【中文标题】Flutter - 无法在 macOS 上启动应用程序进行调试【英文标题】:Flutter - Trouble launching app for debug on macOS 【发布时间】:2019-02-03 11:35:05 【问题描述】:我最近尝试将代码从main.dart
迁移到home_page.dart
的开头,以便对我的应用程序进行流式处理。由于这样做,我的应用程序现在无法编译,我怀疑它不知道在哪里启动应用程序。
这是我收到的错误,Set the 'program' value in your launch config (eg 'bin/main.dart') then launch again
(在 VSCode 中) 但是自从迁移代码后我没有 main.dart
,下面是删除前最后使用的版本,包括home_page.dart
的当前版本
另外,下面是$ flutter doctor -v
的结果
main.dart
;
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:cryptick_nice_ui/dependency_injection.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:cryptick_nice_ui/home_page.dart';
void main() async
Injector.configure(Flavor.PROD);
runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.pink,
primaryColor: defaultTargetPlatform == TargetPlatform.ios
? Colors.grey[100]
: null),
debugShowCheckedModeBanner: false,
home: new HomePage(),
);
home_page.dart
;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cryptick_nice_ui/data/crypto_data.dart';
import 'package:cryptick_nice_ui/modules/crypto_presenter.dart';
import 'background.dart';
import 'package:cryptick_nice_ui/dependency_injection.dart';
void main() async
Injector.configure(Flavor.PROD);
runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.pink,
primaryColor: defaultTargetPlatform == TargetPlatform.iOS
? Colors.grey[100]
: null),
debugShowCheckedModeBanner: false,
home: new HomePage(),
);
class HomePage extends StatefulWidget
@override
_HomePageState createState() => new _HomePageState();
class _HomePageState extends State<HomePage> implements CryptoListViewContract
CryptoListPresenter _presenter;
List<Crypto> _currencies;
bool _isLoading;
final List<MaterialColor> _colors = [Colors.blue, Colors.indigo, Colors.red];
_HomePageState()
_presenter = new CryptoListPresenter(this);
@override
void initState()
super.initState();
_isLoading = true;
_presenter.loadCurrencies();
@override
Widget build(BuildContext context)
return new Scaffold(
appBar: new AppBar(
title: new Text(".",
style: new TextStyle(
fontFamily: 'PlayfairDisplay',
letterSpacing: 0.8,
color: const Color(0xFF273A48),
)
),
backgroundColor: const Color(0xFF273A48),
elevation: 0.0,
),
body: _isLoading
? new Center(
child: new CircularProgressIndicator(),
)
: _cryptoWidget()
);
Widget _cryptoWidget()
final _width = MediaQuery.of(context).size.width;
final _height = MediaQuery.of(context).size.height;
return new Container(
decoration: new BoxDecoration(
color: const Color(0xFF273A48),
),
child: new Column(
children: <Widget>[
new CustomPaint(
size: new Size(_width, _height),
painter: new Background(),
),
new Flexible(
child: new ListView.builder(
itemCount: _currencies.length,
itemBuilder: (BuildContext context, int index)
final int i = index ~/ 2;
final Crypto currency = _currencies[i];
final MaterialColor color = _colors[i % _colors.length];
if (index.isOdd)
return new Divider();
return _getListItemUi(context, currency, color);
,
),
)
],
)
);
Widget _getListItemUi(BuildContext context, Crypto currency, MaterialColor color)
final _width = MediaQuery.of(context).size.width;
final _height = MediaQuery.of(context).size.height;
_presenter.loadCurrencies();
final headerList = new ListView.builder(
itemBuilder: (context, index)
EdgeInsets padding = index == 0?const EdgeInsets.only(
left: 20.0, right: 10.0, top: 4.0, bottom: 30.0):const EdgeInsets.only(
left: 10.0, right: 10.0, top: 4.0, bottom: 30.0);
return new Padding(
padding: padding,
child: new InkWell(
onTap: ()
print('Card selected');
,
child: new Container(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(10.0),
color: Colors.lightGreen,
boxShadow: [
new BoxShadow(
color: Colors.black.withAlpha(70),
offset: const Offset(3.0, 10.0),
blurRadius: 15.0)
],
image: new DecorationImage(
image: new ExactAssetImage(
''),
fit: BoxFit.fitHeight,
),
),
// height: 200.0,
width: 200.0,
child: new Stack(
children: <Widget>[
new Align(
alignment: Alignment.bottomCenter,
child: new Container(
decoration: new BoxDecoration(
color: const Color(0xFF273A48),
borderRadius: new BorderRadius.only(
bottomLeft: new Radius.circular(10.0),
bottomRight: new Radius.circular(10.0))),
height: 30.0,
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'hi',
style: new TextStyle(color: Colors.white),
)
],
)),
)
],
),
),
),
);
,
scrollDirection: Axis.horizontal,
itemCount: _currencies.length,
);
final body = new Scaffold(
appBar: new AppBar(
title: new Text('cryp'),
elevation: 0.0,
backgroundColor: Colors.transparent,
actions: <Widget>[
new IconButton(icon: new Icon(Icons.shopping_cart, color: Colors.white,), onPressed: ())
],
),
backgroundColor: Colors.transparent,
body: new Container(
child: new Stack(
children: <Widget>[
new Padding(
padding: new EdgeInsets.only(top: 10.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Align(
alignment: Alignment.centerLeft,
child: new Padding(
padding: new EdgeInsets.only(left: 8.0),
child: new Text(
'Trending News',
style: new TextStyle(
color: Colors.white70,
fontSize: 15.0,
),
)),
),
new Container(
height: 300.0, width: _width, child: headerList),
new Expanded(child:
ListView.builder(itemBuilder: (context, index)
return new ListTile(
title: new Column(
children: <Widget>[
new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
height: 72.0,
width: 72.0,
decoration: new BoxDecoration(
color: Colors.lightGreen,
boxShadow: [
new BoxShadow(
color:
Colors.black.withAlpha(70),
offset: const Offset(2.0, 2.0),
blurRadius: 2.0)
],
borderRadius: new BorderRadius.all(
new Radius.circular(12.0)),
image: new DecorationImage(
image: new ExactAssetImage(
"cryptoiconsBlack/"+currency.symbol.toLowerCase()+"@2x.png",
),
fit: BoxFit.cover,
)),
),
new SizedBox(
width: 8.0,
),
new Expanded(
child: new Column(
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
new Text(
'My item header',
style: new TextStyle(
fontSize: 14.0,
color: Colors.black87,
fontWeight: FontWeight.bold),
),
new Text(
'Item Subheader goes here\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry',
style: new TextStyle(
fontSize: 12.0,
color: Colors.black54,
fontWeight: FontWeight.normal),
)
],
)),
new Icon(
Icons.shopping_cart,
color: const Color(0xFF273A48),
)
],
),
new Divider(),
],
),
);
))
],
),
),
],
),
),
);
return new Container(
decoration: new BoxDecoration(
color: const Color(0xFF273A48),
),
child: new Stack(
children: <Widget>[
new CustomPaint(
size: new Size(_width, _height),
painter: new Background(),
),
body,
],
),
);
Widget _getSubtitleText(String priceUSD, String percentageChange)
TextSpan priceTextWidget = new TextSpan(
text: "\$$priceUSD\n", style: new TextStyle(color: Colors.black));
String percentageChangeText = "1 hour: $percentageChange%";
TextSpan percentageChangeTextWidget;
if (double.parse(percentageChange) > 0)
percentageChangeTextWidget = new TextSpan(
text: percentageChangeText,
style: new TextStyle(color: Colors.green));
else
percentageChangeTextWidget = new TextSpan(
text: percentageChangeText, style: new TextStyle(color: Colors.red));
return new RichText(
text: new TextSpan(
children: [priceTextWidget, percentageChangeTextWidget]));
@override
void onLoadCryptoComplete(List<Crypto> items)
// TODO: implement onLoadCryptoComplete
setState(()
_currencies = items;
_isLoading = false;
);
@override
void onLoadCryptoError()
// TODO: implement onLoadCryptoError
$flutter doctor -v
的结果
[✓] Flutter (Channel beta, v0.5.1, on Mac OS X 10.13.6 17G65, locale en-GB)
• Flutter version 0.5.1 at /Users/Jake/flutter
• Framework revision c7ea3ca377 (3 months ago), 2018-05-29 21:07:33 +0200
• Engine revision 1ed25ca7b7
• Dart version 2.0.0-dev.58.0.flutter-f981f09760
[✓] android toolchain - develop for Android devices (Android SDK 28.0.2)
• Android SDK at /Users/jake/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 9.4.1, Build version 9F2000
• ios-deploy 1.9.2
• CocoaPods version 1.5.3
[✓] Android Studio (version 3.1)
• Android Studio at /Applications/Android Studio.app/Contents
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
[✓] Connected devices (1 available)
• Nokia 3310 • b689ec530586d0c681cac2136a29548cfbe7c9a6 • ios • iOS 11.4.1
• No issues found!
【问题讨论】:
【参考方案1】:理论上,您可以在“调试”>“打开配置”中更改一个选项以使其运行不同的文件,尽管我没有看到该选项。但即使你知道这一点,它也可能是不可取的。当您执行“构建”来部署应用程序时,它可能会假设 main.dart 是要运行的文件,如果找不到它可能会失败。
我建议保留一个 main.dart - 它可能只有 void main()
函数,而您的其余代码可能在其他地方,但至少其他人正在查看您的代码将能够识别出主要是。
【讨论】:
作为记录,在 IntelliJ 中,您只需右键单击并运行其中包含void main()
的任何文件。
您说它将假定main.dart
是要运行的文件,您知道我该如何更改吗?还是我最好只更改调试>打开配置,正如你所说这并不理想,因为我只会在发布时遇到更多错误。
刚刚找到了一个非常简单的解决方法,我只是将home_page.dart
重命名为前面提到的main.dart
,这会诱使 VSCode 运行该文件进行调试。【参考方案2】:
打开 .vscode/launch.json
在“配置”中添加“程序”
"configurations": [
"name": "Flutter",
"request": "launch",
"type": "dart",
"program": "lib/your_class/file_name_with_main_function.dart"
]
将“lib”替换为文件夹,将“your_class/file_name_with_main_function”替换为包含主函数的类名
【讨论】:
【参考方案3】:当您的 main.dart(您的 Flutter 应用程序的起点)不在您的 /lib 文件夹中时,就会发生这种情况。 如何修复:在您的 launch.json 文件中添加这一行,在“配置”下:
["program": "/$workspaceFolder/lib/screens/home/main.dart"]
【讨论】:
【参考方案4】:我遇到了这样的问题(在 Windows 上)。我通过将以下代码添加到我的launch.json
来修复它:
"program": "/Users/user/classes_advanced/animal.dart"
/Users/user/classes_advanced/animal.dart
是我的文件路径名。只需右键单击要启动的程序并复制 PATH。然后用你的替换我自己的。请确保将斜杠 /
相应地替换为 \
。
【讨论】:
以上是关于Flutter - 无法在 macOS 上启动应用程序进行调试的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:在 MacOs Big Sur (VirtualBox) 上设置 Android Studio 后无法运行程序
Flutter ios Build 无法在 MacOS Big Sur 11.0 Beta 上运行
无法在 macOS Monterey 上启动 Kestrel
Flutter clean 和 flutter run 不会使用 Visual Studio Code 在 macOS 上重建应用程序