如何从Flutter的不同文件中访问类方法?

huangapple go评论66阅读模式
英文:

How can I access class methods from a different file in Flutter?

问题

以下是翻译好的内容:

我在一个名为 `main.dart` 的文件中有一个简单的类 `MyApp`,其中包含两个函数(`setLocale`、`setThemeMode`)。

```dart
class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();

  static State<MyApp> of(BuildContext context) =>
      context.findAncestorStateOfType<_MyAppState>()!;
}

class _MyAppState extends State<MyApp> {

    void setLocale(String language) {}
    void setThemeMode(ThemeMode mode) {}

}

我想在不同文件 flutter_flow.dart 中的函数中使用它们(没有类,只有这些函数)。

void setAppLanguage(BuildContext context, String language) =>
    MyApp.of(context).setLocale(language);

void setDarkModeSetting(BuildContext context, ThemeMode themeMode) =>
    MyApp.of(context).setThemeMode(themeMode);

但我遇到了以下错误。
The method 'setLocale' isn't defined for the type 'State'. Try correcting the name to the name of an existing method, or defining a method named 'setLocale'.

据我理解,文件似乎无法识别这些方法,尽管我已导入包含 MyApp 类的文件。我可以请教一下,应该怎么做,从哪里寻找解决方案?


<details>
<summary>英文:</summary>

I&#39;ve got a simple class `MyApp` in a `main.dart` file, with two functions in it (`setLocale`, `setThemeMode`).

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();

static State<MyApp> of(BuildContext context) =>
context.findAncestorStateOfType<_MyAppState>()!;
}

class _MyAppState extends State<MyApp> {

void setLocale(String language) {}
void setThemeMode(ThemeMode mode) {}

}


And I would like to use them within functions in different file `flutter_flow.dart` (no class, clean file with these functions).

void setAppLanguage(BuildContext context, String language) =>
MyApp.of(context).setLocale(language);

void setDarkModeSetting(BuildContext context, ThemeMode themeMode) =>
MyApp.of(context).setThemeMode(themeMode);


And I&#39;m stuck with this error.
`The method &#39;setLocale&#39; isn&#39;t defined for the type &#39;State&#39;. Try correcting the name to the name of an existing method, or defining a method named &#39;setLocale&#39;.`

As far as I understand, file does not see this methods although I&#39;m importing file with class MyApp. May I ask for help, what to do, where to search for solution?

</details>


# 答案1
**得分**: 0

你需要将以下代码中的返回类型更改为:

```dart
_MyAppState of(BuildContext context)

使用您当前的代码,当您执行以下操作时:

MyApp.of(context)

它返回类型为 State<MyApp>,这是通用的 State 类,来自 Flutter,它不包含任何自定义方法。

当您将 of(BuildContext context) 的返回类型更改为 _MyAppState 时,MyApp.of(context) 将返回 _MyAppState 类型。Dart 分析器知道它具有您的自定义方法 setLocalesetThemeMode,不再会抛出错误。

英文:

You need to change the return type of

State&lt;MyApp&gt; of(BuildContext context)

to

_MyAppState of(BuildContext context)

With your current code, when you do

MyApp.of(context)

it returns the type State&lt;MyApp&gt; which is the generic State class from flutter which won't contain any of your custom methods.

When you change the return of of(BuildContext context) to _MyAppState, MyApp.of(context) will return _MyAppState type. The dart analyzer knows that it has your custom methods setLocale and setThemeMode and won't throw an error anymore.

huangapple
  • 本文由 发表于 2023年5月29日 01:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定