这是我的代码和我在Visual代码中遇到的错误。它以前经常工作,不确定更新是否弄糟了它。我还有其他的颤振程序运行得很好,不知道这个程序有什么问题。
// 1) Create a new Flutter App (in this project) and output an AppBar and some text
// below it
// 2) Add a button which changes the text (to any other text of your choice)
// 3) Split the app into three widgets: App, TextControl & Text
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
Random _stringIndex = new Random();
var _randomStringIndex;
final _strings = const [
"Hello",
"This is a string",
"This is random",
];
void _changeText() {
setState(() {
_randomStringIndex = _stringIndex.nextInt(_strings.length);
});
print(_strings[_randomStringIndex]);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: _changeText,
),
Text(
_strings[_randomStringIndex],
),
],
),
),
),
),
);
}
}在Android上启动lib/main.DAT在调试模式下为x86构建.✓构建了build/app/outputs/apk/debug/app-调试器。连接到VM服务的位置为ws://127.0.0.1:40605/I0TqLbk5jw4=/ws D/EGL_ to ( 7151):eglMakeCurrent: 0xe1b1a3c0: ver2.0 (tinfo 0xe1b0f770) D/eglCodecCommon7151: setVertexArrayObject:将vao设置为0 (0) 1.0 I/flutter ( 7151):由小部件库╞═══════════════════════════════════════════捕获的══╡异常ArgumentError I/════════════════( 7151):抛出以下════════════════构建MyApp(脏,状态:_MyAppState#202c3):I/颤振( 7151):无效的参数I/颤振( 7151):I/颤振( 7151):相关的导致错误的小部件是:I/颤振( 7151):MyApp I/颤振( 7151):
程序包:_MyAppState.build _assignment/main.dart:11I/_MyAppState.build( 7151):I/flutter ( 7151):当抛出异常时,这是堆栈:i/颤振( 7151):#0列表。
StatefulElement.build包:颤振/…/widgets/Frawork.DART:4619 I/flutter ( 7151):#3 ComponentElement.performRebuild包:flutter/…/widget/Frawork.DAT:4502 I/颤振( 7151):#4
StatefulElement.performRebuild包:颤振/…/widget/Frawork.DAT:4675 I/颤振( 7151):#5
Element.rebuild包:颤振/…/widgets/Frawork.DART:4218 I/flutter ( 7151):#6 ComponentElement._firstBuild包:flutter/…/widget/Frawork.DAT:4481 I/颤振( 7151):#7
StatefulElement._firstBuild包:颤振/…/widget/Frawork.DART:4666 I/颤振( 7151):#8
ComponentElement.mount包:颤振/…/widgets/Frawork.DART:4476I/flutter( 7151):#9 Element.inflateWidget包:flutter/…/widget/Frawork.DAT:3446 I/颤振( 7151):#10
Element.updateChild包:颤振/…/widgets/Frawork.DART:3214 I/flutter ( 7151):#11 RenderObjectToWidgetElement._rebuild包:flutter/…/widget/binding.dart:1148 I/颤振( 7151):#12
RenderObjectToWidgetElement.mount包:颤振/…/widget/binding.dart:1119 I/颤振( 7151):#13
RenderObjectToWidgetAdapter.attachToRenderTree.包装:颤振/…/widget/binding.dart:1061 I/颤振( 7151):#14
BuildOwner.buildScope包:颤振/…/widget/Frawork.DAT:2607 I/颤振( 7151):#15
RenderObjectToWidgetAdapter.attachToRenderTree包:颤振/…/widget/binding.dart:1060 I/颤振( 7151):#16
WidgetsBinding.attachRootWidget包:颤振/…/widget/binding.dart:941
发布于 2020-09-24 00:51:10
您可以复制粘贴,运行下面的完整代码
您需要插入_randomStringIndex
代码段
var _randomStringIndex = 0;工作演示

全码
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
Random _stringIndex = new Random();
var _randomStringIndex = 0;
final _strings = const [
"Hello",
"This is a string",
"This is random",
];
void _changeText() {
setState(() {
_randomStringIndex = _stringIndex.nextInt(_strings.length);
});
print(_strings[_randomStringIndex]);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: _changeText,
),
Text(
_strings[_randomStringIndex],
),
],
),
),
),
),
);
}
}发布于 2020-09-24 00:10:56
尝试在项目(终端)中运行此命令:
flutter clean然后再运行一次!
https://stackoverflow.com/questions/64037706
复制相似问题