【问题标题】:Flutter: Undefined name Try correcting the name to one that is defined, or defining the nameFlutter:未定义名称尝试将名称更正为已定义的名称,或定义名称
【发布时间】:2020-09-03 11:04:54
【问题描述】:

我在使用 Flutter 时遇到了一些奇怪的问题。我对 Flutter 知之甚少。我正在学习它。

class ViewOtherProfile extends StatefulWidget {
  final String userName;
  final int points;

  const ViewOtherProfile({
    @required this.userName,
    @required this.points,
  });

您可以看到我将 userName 和 Points 数据作为参数。 我想在页面中打印这个参数。像这样

class _ViewOtherProfileState extends State<ViewOtherProfile> {
..........

void initState(){ 
print(points);

     deviceInfo();
    super.initState();
     print(userName);
    ]);
   } 
    ............   

现在的问题是我遇到了错误。

Undefined name 'userName'.
Try correcting the name to one that is defined, or defining the name.

我收到此错误的任何原因以及如何解决它。

感谢@jamesdlin

我试着这样写

print(ViewOtherProfile.userName); 

但现在我又遇到了另一个错误。

Instance member 'userName' can't be accessed using static access.

【问题讨论】:

  • userNameViewOtherProfile 的成员,而不是_ViewOtherProfileState 的成员。您可以使用widget.userName_ViewOtherProfileState 访问它。
  • @jamesdlin 感谢您的建议,任何示例都会非常有帮助。我不了解widget.userName。
  • jamesdlin 的意思是 userName 是小部件的一部分,即 ViewOtherProfile,而不是状态对象 (_ViewOtherProfileState)。为了在状态对象中使用 userName,您可以通过执行 widget.userName 从小部件中获取它
  • @Unbreachable 感谢您的澄清。我试过 print(ViewOtherProfile.userName);但现在我可以在打印和用户名上看到错误。无法使用静态访问访问实例成员“用户名”。
  • 不,ViewOtherProfile.userName 不正确。 ViewOtherProfile is 具有您声明的属性的有状态小部件本身。已经澄清你像这样使用它 widget.userName

标签: flutter dart


【解决方案1】:

Flutter 中有两种主要类型的小部件。 StatelessWidgetStatefullWidget。 StatelessWidget 仅在 UI 构建时构建,并且永远不会重新构建。同时,如果调用setState,每次都可以重建一个StatefulWidget。

因此,对于 StatefulWiget,需要通过使用 State 类扩展主类来跟踪类的状态。

您必须注意,这两种类型的小部件中的变量范围可能会有所不同。比如……

class ExampleStateless extends StatelessWidget {

    final String userName;
    final int points; 

   const ExampleStateless({
    @required this.userName,
    @required this.points,
   });

   @override
   Widget build(BuildContext context){
      print(userName); print(points);  

      return Something();
   }
}

请注意,对于有状态小部件,有两个类,每个类都有其范围。超类ExampleStateful 可以通过widget 将其范围共享给它的子类_ExampleStatefulState

class ExampleStateful extends StatefulWidget {
    final String userName;
    final int points; 
    Static final string id = "exampleState"; 

   const ExampleStatefull({
    @required this.userName,
    @required this.points,
   });
   // scope 1 
 @override
  _ExampleStatefulState createState() => _ExampleStatefulState();
}

class _ExampleStatefulState extends State<ExampleStateful>{
  // scope 2 

 final String userName2 = null;

@override
void initState(){
   super.initState();
   print(widget.userName);
   print(userName2);
   print(ExampleStateful.id);  // you can do this only if variable is static. 
 }
}

范围 1 中的内容可以通过小部件属性在范围 2 中访问。例如print(widget.userName); 而不是 print(userName);

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 2022-08-22
    • 2022-06-10
    • 1970-01-01
    • 2021-11-28
    • 2020-01-28
    • 2020-11-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多