参数类型“TextStyle”无法分配给参数类型“TextStyle?”。调用返回 TextStyle 的函数时

问题描述 投票:0回答:1

我创建了一个名为

getRegularStyle
的函数,它返回
TextStyle
。当我想用它来 change
appBar
titleTextStyle
时,它会给出以下错误:
The argument type 'TextStyle' can't be assigned to the parameter type 'TextStyle?'.
这是我的功能

TextStyle _getTextStyle(double fontSize, String fontFamily, FontWeight fontWeight, Color color){
  return TextStyle(fontSize: fontSize, fontFamily: fontFamily, fontWeight: fontWeight, color: color);
}
//Regular Style
TextStyle getRegularStyle({double fontSize = 12, required Color color}){
  return _getTextStyle(fontSize, FontConstants.fontFamily, FontWeightManager.regular, color);
}

这里是我调用函数来更改 appBartitleTextStyle 的地方:

appBarTheme: AppBarTheme(
    centerTitle: true,
    color: ColorManager.primary,
    elevation: AppSize.s4,
    shadowColor: ColorManager.primaryWithOpacity70,
    **titleTextStyle: getRegularStyle(color: ColorManager.white, fontSize: FontSize.s16),**
),

粗体代码行提供了错误。

我什至使函数可以为空来解决问题,但它提供了另一个错误,在我的情况下,我认为这不能解决问题。

flutter flutter-dependencies flutter-animation
1个回答
0
投票

最有可能的是,您的

getRegularStyle
AppBarTheme
位于不同的文件中,并且它们导入了不同的包。

例如

import 'dart:ui';

//Regular Style
TextStyle getRegularStyle({double fontSize = 12, required Color color}){
  return _getTextStyle(fontSize, FontConstants.fontFamily, FontWeightManager.regular, color);
}

import 'package:flutter/material.dart';

appBarTheme: AppBarTheme(
    centerTitle: true,
    color: ColorManager.primary,
    elevation: AppSize.s4,
    shadowColor: ColorManager.primaryWithOpacity70,
    **titleTextStyle: getRegularStyle(color: ColorManager.white, fontSize: FontSize.s16),**
),

一个导入

dart.ui
,而另一个导入
flutter/material.dart
(或其他方式)。这将导致参数类型无法分配错误。检查您的导入或粘贴完整的代码文件。

© www.soinside.com 2019 - 2024. All rights reserved.