执行报错: 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回答

CrazyCodeBoy

2022-02-22

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

1
1
慕无忌6890528
这里去掉 const 关键字之后是可以的。 老师可以讲解一下为什么吗 ?
2022-02-23
共1条回复

CrazyCodeBoy

2022-02-24

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

0
0

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3277 问题

查看课程