You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter报错:Body.new需1个位置参数但传入0个,求解决

问题:Flutter电商应用启动报错:1 positional argument expected by 'Body.new' but 0 found

1 positional argument expected by 'Body.new' but 0 found

splash_screen.dart 文件内容

import 'package:flutter/material.dart';
import 'package:shop/screens/components/body.dart';
import 'package:shop/size_config.dart';

class SplashScreen extends StatelessWidget {
  static var routeName;

  const SplashScreen({super.key});

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Scaffold(
      body: Body(),
    );
  }
}

body.dart 文件内容

import 'package:flutter/material.dart';
import 'package:shop/constants.dart';
import 'package:shop/screens/components/splash_content.dart';
import 'package:shop/screens/sign_in/sign_in_screen.dart';
import 'package:shop/size_config.dart';

class Body extends StatefulWidget {
  const Body(SafeArea Function(BuildContext context) param0, {super.key});

  @override
  State<Body> createState() => _BodyState();
}

class _BodyState extends State<Body> {
  int currentPage = 0;
  List<Map<String, String>> splashData = [
    {
      "text": "Welcome to Amaze, Let's Shop!",
      "image": "assets/images/splash_1.png",
    },
    {
      "text": "We help people to connect with stores\n across India",
      "image": "assets/images/splash_2.png",
    },
    {
      "text": "Just stay home with us",
      "image": "assets/images/splash_3.png",
    },
  ];
  @override
  Widget build(BuildContext context) {
    const Spacer(
      flex: 2,
    );
    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 3,
              child: PageView.builder(
                onPageChanged: (value) {
                  setState(() {
                    currentPage = value;
                  });
                },
                itemCount: splashData.length,
                itemBuilder: (context, index) => SplashContent(
                  image: splashData[index]['image']!,
                  text: "Welcome to amaze, Lets Shop!",
                  key: UniqueKey(),
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: Column(
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: List.generate(
                      splashData.length,
                      (index) => buildDot(index: index),
                    ),
                  ),
                  const Spacer(
                    flex: 10,
                  ),
                  Container(
                    margin: const EdgeInsets.all(30),
                    child: SizedBox(
                      width: getPropotionateScreenWidth(300),
                      height: getPropotionateScreenHeight(56),
                      child: FloatingActionButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                        backgroundColor: kPrimaryColor,
                        onPressed: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => SignInScreen(),
                            ),
                          );
                        },
                        child: Text(
                          "Continue",
                          style: TextStyle(
                            fontSize: getPropotionateScreenWidth(18),
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  AnimatedContainer buildDot({required int index}) {
    return AnimatedContainer(
      duration: kAnimationDuration,
      margin: const EdgeInsets.only(right: 5),
      height: 6,
      width: currentPage == index ? 15 : 5,
      decoration: BoxDecoration(
        color: currentPage == index ? kPrimaryColor : Color(0xFFD8D8D8),
        borderRadius: BorderRadius.circular(3),
      ),
    );
  }
}

该错误在首次运行程序时并未出现,寻求解决办法。


解决方案

错误原因

Body类的构造函数被定义为需要接收一个SafeArea Function(BuildContext context)类型的位置参数,但在splash_screen.dart中实例化Body()时并未传入该参数。同时从body.dart的代码逻辑来看,这个参数并没有被实际使用,属于冗余定义。

修复步骤

修改body.dart中的Body构造函数,移除多余的位置参数:

class Body extends StatefulWidget {
  const Body({super.key}); // 移除无用的param0参数

  @override
  State<Body> createState() => _BodyState();
}

修改后,保持splash_screen.dart中的Body()实例化代码不变即可,程序就能正常运行。

补充说明

首次运行未报错,大概率是因为最初编写Body类时没有添加这个冗余参数,后续修改body.dart时误加了该参数,但没有同步更新调用处的代码,从而触发了这个参数不匹配的错误。

内容的提问来源于stack exchange,提问作者Abhinav Punnakkan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:35:12