Flutter无法识别有效进口

时间:2019-03-05 22:28:12

标签: dart flutter

我有这个简单的Flutter应用。

import 'package:flutter/material.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sample'),
        ),
        body: Center(
          child: Column (
            children: <Widget>[
              CustomCard(mainTitle: "A simple widget!"),
            ]
          )
        ),
      ),
    );
  }
}

class CustomCard extends StatelessWidget {
  final String mainTitle;

  CustomCard({@required this.mainTitle});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 48,
      width: 200,
      child: Text('$mainTitle'),
    );
  }
}

如果我将CustomCard类移到另一个文件中,例如custom_card.dart,然后执行import 'package:myproject/custom_card.dart',我收到错误消息:"The parameter mainTitle is not defined"。如果类CustomCard保留在main.dart中,则一切正常。 我想念什么?

这是我的设置。 抖动1.2.1•通道稳定•https://github.com/flutter/flutter.git 框架•修订版8661d8aecd(3周前)•2019-02-14 19:19:53 -0800 发动机•版本3757390fa4 工具•Dart 2.1.2(内部版本2.1.2-dev.0.0 0a7dcf17eb)• Windows 10 64x

1 个答案:

答案 0 :(得分:0)

package:myproject/custom_card.dart是对程序包根目录下的lib/custom_card.dart的引用。您还需要确保myproject部分与pubspec.yaml文件中的软件包名称匹配。参见https://www.dartlang.org/tools/pub/package-layout#public-libraries