【问题标题】:Expected a value of type 'string' but got one of type 'int' - Flutter期望一个“string”类型的值,但得到一个“int”类型的值 - Flutter
【发布时间】:2021-11-26 02:26:03
【问题描述】:

我有一个返回 String 的函数,但是当我调用此函数时,应用程序屏幕变红并出现以下错误:Expected a value of type 'string' but got one of type 'int'

这是我返回 String 的函数:

  checkProportion(String predominantGamete, String resultado) {
    var countBrown = 0;
    var countBlack = 0;
    var countWhite = 0;
    var proportionCamundongo =
        'Proporção: ${countBrown}:${countBlack}:${countWhite}';

    if (predominantGamete == 'recessiva_aa' &&
        resultado.contains('A') &&
        resultado.contains('P')) {
      return countBrown += 1;
    } else if (predominantGamete == 'recessiva_aa' &&
        resultado.contains('A') &&
        resultado.contains('pp')) {
      return countBlack += 1;
    } else if (predominantGamete == 'recessiva_aa' &&
        resultado.contains('aa')) {
      return countWhite += 1;
    }
    return proportionCamundongo;
  }

这是我调用函数的方式:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        title: Text(
          checkProportion(widget.predominant, widget.result),
        ),
      ),

如何解决这个错误?

这是显示每个结果的颜色的图像:

【问题讨论】:

    标签: flutter dart mobile


    【解决方案1】:

    这里的问题是,当您执行return countBrown += 1; 之类的操作时,您会提前返回,而不是破坏 if 语句;

    尝试递增计数器,然后使用字符串插值来显示值:

    String checkProportion(String predominantGamete, String resultado) {
      int countBrown = 0;
      int countBlack = 0;
      int countWhite = 0;
    
      if (predominantGamete == 'recessiva_aa' &&
        resultado.contains('A') &&
        resultado.contains('P')) {
        countBrown += 1;
      } else if (predominantGamete == 'recessiva_aa' &&
        resultado.contains('A') &&
        resultado.contains('pp')) {
        countBlack += 1;
      } else if (predominantGamete == 'recessiva_aa' &&
        resultado.contains('aa')) {
        countWhite += 1;
      }
      return 'Proporção: ${countBrown}:${countBlack}:${countWhite}';
    }
    

    我还建议指定函数的返回类型 (String),使用正确的计数器类型 (int)。这也将帮助您的编译器发现问题。

    【讨论】:

    • 这很好用,但我想知道是否有办法将每个 if 语句转换为具有相同条件的 for 循环。它没有给我想要的结果,它只是计数了一次,仅此而已。你能帮我看看吗?
    【解决方案2】:

    这不是我最好的工作,可能有更好的方法来检查一个字符串是否包含所有出现的多个子字符串,但是你去吧:

    bool isColorContained(String resultado, Set<String> requirements) {
      for(String requirement in requirements) {
          if (!resultado.contains(requirement)) {
            return false;            
          }
        }
      return true;
    }
    

     

    String checkProportion(String predominantGamete, String resultado) {         
      Map<ColorType, Set<String>> colorType = {
        ColorType.brown: {'A', 'P'},
        ColorType.black: {'A', 'pp'},
        ColorType.white: {'aa'},
      };
      Map<ColorType, int> colorTypeCount = {
        ColorType.brown: 0,
        ColorType.black: 0,
        ColorType.white: 0,
      };
      for(MapEntry<ColorType, Set<String>> entry in colorType.entries )  {
        if(predominantGamete != 'recessiva_aa') continue;
        bool contained = isColorContained(resultado, entry.value);        
        if(contained) {
          int count = colorTypeCount[entry.key] ?? 0;
          colorTypeCount[entry.key] = count + 1;
        }      
      }
      return 'Proporção: ${colorTypeCount[ColorType.brown]}:${colorTypeCount[ColorType.black]}:${colorTypeCount[ColorType.white]}';
    }
    

    另外,声明ColorType 枚举:

    enum ColorType {
      brown, black, white
    }
    

    通过添加到 ColorType 枚举、colorType 映射和 colorTypeCount 映射,这将根据您的颜色和要求进行缩放。

    【讨论】:

    • 这里给出了 ColorType 的错误The name 'ColorType' isn't a type so it can't be used as a type argument. Try correcting the name to an existing type, or defining a type named 'ColorType'.
    • 确保你定义了ColorType(它是函数后面的枚举)。您需要在任何函数或类之外执行此操作。
    • 另外,我通过添加 isColorContained 函数稍微清理了该函数
    • 我很抱歉问这个问题,但是,我将如何定义 ColorType?我有点卡住了
    • 在 dart enums 上查看这个:dart.dev/guides/language/language-tour#enumerated-types 底线,是你只需要在你的类或定义之前或之后添加这段代码(它不能被声明为 inside 一个类或函数):enum ColorType { brown, black, white }
    猜你喜欢
    • 2022-12-05
    • 2020-11-12
    • 2022-01-03
    • 2021-12-27
    • 2021-03-16
    • 2022-01-08
    • 2021-09-13
    • 2021-09-18
    • 2020-12-03
    相关资源
    最近更新 更多