【问题标题】:How to fix Flutter MaterialApp(not_enough_required_arguments)?如何修复 Flutter MaterialApp(not_enough_required_arguments)?
【发布时间】:2018-12-06 13:00:35
【问题描述】:

当返回 MaterialApp 时,颤振显示错误如下“错误:预期 1 个必需参数,但找到 0 个。(not_enough_required_arguments at [demo] lib/main.dart:9)”Here is the screenshot

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,
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 最好把代码粘贴到这里,而不是添加截图

标签: flutter flutter-layout


【解决方案1】:

您应该将 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'),
          ),
       ),
    );
  }
}

【讨论】:

  • 谢谢,但我不想要 appbar,我正在构建一个没有 appbar 的布局。
【解决方案2】:

如果我删除 Category() 小部件,您的代码工作正常且没有错误。下面的代码工作正常:

import 'package:flutter/material.dart';

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

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: Container(),
        ),
      ),
    );
  }
}

也许问题出在 Category() 小部件中,所以如果您发布它,我可以为您检查。

【讨论】:

  • 但是我在新的颤振项目中遇到了同样的错误。
  • 停止模拟器,运行 Flutter Clean 然后尝试重建应用程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2018-07-07
  • 2018-09-04
  • 1970-01-01
相关资源
最近更新 更多