英文:
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'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'm stuck with this error.
`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'.`
As far as I understand, file does not see this methods although I'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 分析器知道它具有您的自定义方法 setLocale
和 setThemeMode
,不再会抛出错误。
英文:
You need to change the return type of
State<MyApp> of(BuildContext context)
to
_MyAppState of(BuildContext context)
With your current code, when you do
MyApp.of(context)
it returns the type State<MyApp>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论