Flutter:为什么要重命名"myapp";符号到"计时器";在visual



我正在使用颤振代码实现简单的计时器应用程序,但更改名称创建4个错误,错误消息如下:

  1. 未使用import: 'dart:async'。
  2. 方法'cancel'没有为'Timer'类型定义。nTry将名称更正为现有方法的名称,或者定义一个名为'cancel'的方法。
  3. 方法'periodic'没有为'Timer'类型定义。nTry将名称更正为现有方法的名称,或定义名为"periodic"的方法。
  4. 方法'cancel'没有为'Timer'类型定义。nTry将名称更正为现有方法的名称,或者定义一个名为'cancel'的方法。

我的代码如下:

import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp()); //change MyApp to Timer
class MyApp extends StatelessWidget { //change MyApp to Timer
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 10;
Timer _timer;
void _startTimer() {
_counter = 10;
if (_timer != null) {
_timer.cancel();
}
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if (_counter > 0) {
_counter--;
} else {
_timer.cancel();
}
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Timer App"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
(_counter > 0)
? Text("")
: Text(
"DONE!",
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 48,
),
),
Text(
'$_counter',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 48,
),
),
RaisedButton(
onPressed: () => _startTimer(),
child: Text("Start 10 second count down"),
),
],
),
),
);
}
}

在'dart:async'中已经存在一个名为' timer '的类。在导入它时,不能有两个具有相同名称的类。因为它是一个抽象类,而code希望你重写那些方法。

最新更新