请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

执行报错: Error: Method invocation is not a constant expression.

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回答

CrazyCodeBoy 2022-02-22 22:17:42

将Text前的const关键字去掉试试看

1 回复 有任何疑惑可以回复我~
  • 提问者 慕无忌6890528 #1
    这里去掉 const 关键字之后是可以的。
    老师可以讲解一下为什么吗 ?
    回复 有任何疑惑可以回复我~ 2022-02-23 16:21:15
CrazyCodeBoy 2022-02-24 09:21:43

const 修饰的widget不能调用方法,这是语法的限制。

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号