如何修复Flutter MaterialApp(not_enough_required_arguments)?

时间:2018-12-06 13:00:35

标签: flutter flutter-layout

返回MaterialApp抖动时显示错误,如下所示:“错误:预期有1个必需参数,但找到0。(位于[demo] lib / main.dart:9处的not_enough_required_arguments)” 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,
          ),
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:1)

您应该将title参数包装在AppBar小部件下。您可以依次将appBar参数包装在支架中。支架就像一个空白页面,为您的应用程序提供白色背景。以下是示例代码实现:

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

答案 1 :(得分:0)

如果我删除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()小部件中,所以如果您发布它,我可以为您检查一下。