简体   繁体   English

Dart 扩展 - 获取“不是类型”错误

[英]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.

相关问题 Dart 扩展 - 没有为类型“xxx”定义方法“xxx” - Dart extension - The method 'xxx' isn't defined for the type 'xxx' 错误:没有为“String”类型定义运算符“&gt;”。 dart flutter - error: The operator '>' isn't defined for the type 'String'. dart flutter Dart Flutter 未为类型“sqliteDaatabase”定义方法“getDatabasePath”。 错误 - Dart Flutter The method 'getDatabasePath' isn't defined for the type 'sqliteDaatabase'. Error Dart 枚举扩展未更新 - Dart enum extension isn't updating Dart:没有为“Type”类型定义方法 - Dart: method isn't defined for the type 'Type' 错误 DART:名称“x”不是类型,因此不能用作类型参数 - ERROR DART: The name 'x' isn't a type so it can't be used as a type argument getter 'fromJson' 没有为 Dart 中的类型定义 - The getter 'fromJson' isn't defined for the type in Dart 我收到此错误“未为“未来”类型定义运算符“[]”<dynamic> &#39;.&quot; 有人可以帮我修复我的代码吗?颤振/飞镖 - I'm getting this error "The operator '[]' isn't defined for the type 'Future<dynamic>'." can someone please help me fix my code? flutter/dart 在遵循 Flutter Firebase 教程时,我在 dart 中收到未为对象错误定义的 [] 错误? - I'm getting an [] isn't defined for Object error in dart when following a Flutter Firebase tutorial? 没有为“String”类型定义运算符“&gt;”。 尝试定义运算符&#39;&gt;&#39;。 飞镖错误 - The operator '>' isn't defined for the type 'String'. Try defining the operator '>'. error in dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM