执行报错: Error: Method invocation is not a constant expression.
来源:6-3 如何使用Flutter包和插件?【轻松应对各种插件】

慕无忌6890528
2022-02-22
plugin_use.dart 源代码:
import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
// void main() {
// runApp(const PluginUse());
// }
class PluginUse extends StatelessWidget {
const PluginUse({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '如何使用 Flutter 插件',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: '如何使用 Flutter 插件'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times-1:',
// ColorUtil.color('#a1FF5733')
// style: TextStyle(color: ColorUtil.color('#899900')),
style: TextStyle(color: ColorUtil.color('#a9ee00')),
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
plugin_use.dart 源代码:
import 'package:flutter/material.dart';
import 'package:flutter_basics_mu_01/plugin_use.dart';
// void main() {
// // runApp(const MyApp());
// runApp(PluginUse)
// }
void main() => runApp(PluginUse());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times-1:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
报错图例:
希望老师能够说一下错误的 具体原因, 因为将老师源码中的代码复制粘贴到相应的文件之后是没有报错的, 但是这样不知道错误的具体原因以后遇到类似的问题自己还是没有办法解决, 谢谢。
写回答
2回答
-
将Text前的const关键字去掉试试看
112022-02-23 -
CrazyCodeBoy
2022-02-24
const 修饰的widget不能调用方法,这是语法的限制。
00
相似问题