[英]Dart extension - Getting "isn't a type" error
I'm trying to extend the BuildContext, more for my learning than anything else.我正在尝试扩展 BuildContext,更多的是为了我的学习。 However, I get this error:
但是,我收到此错误:
AppBuildContext isn't a type.
AppBuildContext 不是一种类型。 Try correcting the name to match an existing type
尝试更正名称以匹配现有类型
I have added this to the file that I'm using it in:我已将此添加到我正在使用它的文件中:
extension AppBuildContext on BuildContext {
ThemeData get theme {
return Theme.of(this);
}
FocusScopeNode get focusScope {
return FocusScope.of(this);
}
NavigatorState get navigator {
return Navigator.of(this);
}
T args<T>() {
return ModalRoute.of(this).settings.arguments as T;
}
}
I use it like this:我像这样使用它:
class LoginView extends StatelessWidget {
@override
Widget build(AppBuildContext context) {
return Scaffold(
body: Center(
child: VpGradientContainer(
beginColor: initialGradientColor,
endColor: Theme.of(context).colorScheme.primary,
child: Column(...
What am I doing wrong?我究竟做错了什么?
Flutter and dart versions: Flutter 和 dart 版本:
Flutter 1.22.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 84f3d28555 (3 days ago) • 2020-10-15 16:26:19 -0700
Engine • revision b8752bbfff
Tools • Dart 2.10.2
The AppBuildContext
name is only for declaring importing restrictions. AppBuildContext
名称仅用于声明导入限制。 for using it, you still call BuildContext
.使用它,你仍然调用
BuildContext
。
import 'AppBuildContext.dart'; // import the file that contains the extension manully
class LoginView extends StatelessWidget {
@override
Widget build(BuildContext context) { // just use normal BuildContext
return Scaffold(
body: Center(
child: VpGradientContainer(
beginColor: initialGradientColor,
endColor: context.theme.colorScheme.primary,
child: Column(...),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.