首页 > 解决方案 > 如何修复 Flutter MaterialApp(not_enough_required_arguments)?

问题描述

当返回 MaterialApp 颤振显示错误如下“错误:1 个必需的参数(s)预期,但 0 找到。(not_enough_required_arguments at [demo] lib/main.dart:9)”这是截图

const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;


import 'package:solution_02_category_widget/category.dart';

void main() {
  runApp(UnitConverterApp());
}

class UnitConverterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Unit Converter',
  home: Scaffold(
        backgroundColor: Colors.green[100],
        body: Center(
          child: Category(
            name: _categoryName,
            color: _categoryColor,
            iconLocation: _categoryIcon,
          ),
        ),
      ),
    );
  }
}

标签: flutterflutter-layout

解决方案


您应该将 title 参数包装在 AppBar Widget 下。您可以反过来将 appBar 参数包装在 Scaffold 中。脚手架就像一个空白页面,为您的应用程序提供白色背景。下面是一个示例代码实现:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // debugShowMaterialGrid: true,
      theme: ThemeData(
          brightness: Brightness .light,
          accentColor: Colors.deepPurple,
          primarySwatch: Colors.deepOrange),
      home: Scaffold(
          appBar: AppBar(
            title: Text('Some text'),
          ),
       ),
    );
  }
}

推荐阅读