首页 > 解决方案 > 背景:找到了这个候选人,但参数不匹配

问题描述

我是 Flutter 的新手,我有 2 个屏幕,我已经尝试了很多方法来解决问题并让这段代码运行,当我尝试将构造函数中的参数推送到 BmiResultScreen 时出现错误

BmiScreen 代码(我需要从中获取值):

     import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'bmi_result_screen.dart';

class BmiScreen extends StatefulWidget {
  @override
  _BmiScreenState createState() => _BmiScreenState();
}

class _BmiScreenState extends State<BmiScreen> {
  bool isMale = true;
  double height = 120.0;
  int age = 0;
  double weight = 0.0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BMI Calculator'),
      ),
      body: Column(
        children: [
          Expanded(
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Row(children: [
                  Expanded(
                    child: GestureDetector(
                      onTap: (){
                        setState(() {
                          isMale = true;
                        });
                      },
                      child: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: const [
                            Image(
                              image: AssetImage('assets/images/male.png'),
                              height: 90.0,
                              width: 90.0,
                            ),
                            SizedBox(
                              height: 15.0,
                            ),
                            Text('MALE',
                                style: TextStyle(
                                    fontSize: 25.0, fontWeight: FontWeight.bold))
                          ],
                        ),
                        decoration: BoxDecoration(
                            color: isMale ? Colors.blue : Colors.grey[400],
                            borderRadius: BorderRadiusDirectional.circular(10.0)),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 20.0,
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        setState(() {
                          isMale = false;
                        });
                      },
                      child: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: const [
                            Image(
                              image: AssetImage('assets/images/female.png'),
                              height: 90,
                              width: 90,
                            ),
                            SizedBox(
                              height: 15.0,
                            ),
                            Text('FEMALE',
                                style: TextStyle(
                                    fontSize: 25.0, fontWeight: FontWeight.bold))
                          ],
                        ),
                        decoration: BoxDecoration(
                            color: !isMale ? Colors.blue : Colors.grey[400],
                            borderRadius: BorderRadiusDirectional.circular(10.0)),
                      ),
                    ),
                  ),
                ]),
              )),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(
                  horizontal: 20.0
              ),
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.grey[400],
                    borderRadius: BorderRadiusDirectional.circular(10.0)
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('HEIGHT',
                        style:
                        TextStyle(fontSize: 25.0, fontWeight: FontWeight.bold)),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.baseline,
                      textBaseline: TextBaseline.alphabetic,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                            '${height.round()}',
                            style: TextStyle(
                              fontSize: 50.0,
                              fontWeight: FontWeight.w900,
                            )
                        ),
                        SizedBox(
                          width: 5.0,
                        ),
                        Text(
                          'cm',
                          style: TextStyle(
                              fontSize: 20.0,
                              fontWeight: FontWeight.w900
                          ),
                        ),
                      ],
                    ),
                    Slider(
                      value: height,
                      max: 220.0,
                      min: 80.0,
                      onChanged: (value) {
                        setState(() {
                          height = value;
                        });
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(20.0),
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.grey[400],
                          borderRadius: BorderRadiusDirectional.circular(10.0)
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                              'WEIGHT',
                              style: TextStyle(
                                fontSize: 25.0,
                                fontWeight: FontWeight.bold,
                              )
                          ),
                          SizedBox(
                            height: 15.0,
                          ),
                          Text(
                              '${weight}',
                              style: TextStyle(
                                fontSize: 50.0,
                                fontWeight: FontWeight.w900,
                              )
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              FloatingActionButton(
                                onPressed: () {
                                  setState(() {
                                    if(weight>=0.5){
                                      weight = weight -0.5;
                                    }
                                  });
                                },
                                child: Icon(
                                    Icons.remove
                                ),
                                mini: true,
                                heroTag: '--weight',
                              ),
                              FloatingActionButton(
                                onPressed: () {
                                  setState(() {
                                    weight = weight + 0.5;
                                  });
                                },
                                child: Icon(
                                    Icons.add
                                ),
                                mini: true,
                                heroTag: '++weight',
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 20.0,
                  ),
                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.grey[400],
                          borderRadius: BorderRadiusDirectional.circular(10.0)
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                              'AGE',
                              style: TextStyle(
                                fontSize: 25.0,
                                fontWeight: FontWeight.bold,
                              )
                          ),
                          SizedBox(
                            height: 15.0,
                          ),
                          Text(
                              '${age}',
                              style: TextStyle(
                                fontSize: 50.0,
                                fontWeight: FontWeight.w900,
                              )
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              FloatingActionButton(
                                onPressed: () {
                                  setState(() {
                                    if(age >= 1){
                                      --age;
                                    }
                                  });
                                },
                                child: Icon(
                                    Icons.remove
                                ),
                                mini: true,
                                heroTag: '--age',
                              ),
                              FloatingActionButton(
                                onPressed: () {
                                  setState(() {
                                    ++age;
                                  });
                                },
                                child: Icon(
                                    Icons.add
                                ),
                                mini: true,
                                heroTag: '++age',
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            color: Colors.blue,
            width: double.infinity,
            height: 50.0,
            child: MaterialButton(
              onPressed: () {
                double result = weight / pow(height/100,2);
                print(result.round());
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => BmiResultScreen(
                        isMale: isMale,
                        age: age,
                        result: result.round(),
                      ),
                  )
                );
              },
              child: const Text('CALCULATE'),
            ),
          ),
        ],
      ),
    );
  }
}

BmiResultScreen 代码(我尝试使用的值来自最后一个屏幕):

    import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

class BmiResultScreen extends StatelessWidget {
   final bool isMale;
   final int result;
   final int age;

  BmiResultScreen({
    required this.isMale,
    required this.age,
    required this.result,
});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
            'BMI Result'
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Gender: $isMale',
              style: TextStyle(
                fontSize: 25.0,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              'Result: ${result.round()}',
              style: TextStyle(
                fontSize: 25.0,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              'Age: $age',
              style: TextStyle(
                fontSize: 25.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      )
    );
  }
}

标签: flutterconstructor

解决方案


可以通过升级您的颤振版本或清理来解决此类问题。
flutter upgrade --force
flutter clean
删除PodfilePodfile.lock
运行pub get
flutter run


推荐阅读