下划线"_"是什么意思?在变量名称之前表示Flutter [英] What does Underscore "_" before variable name mean for Flutter

查看:232
本文介绍了下划线"_"是什么意思?在变量名称之前表示Flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考 Flutter 教程,我遇到了下划线_.

with reference to the Flutter tutorial, I encountered an underscore, _.

我知道在Java中,_用作私有变量的命名约定.

I know that in Java, _ is used as a naming convention for a private variable.

  1. 它也适用于Flutter吗?注意Flutter中没有公共/受保护的对象.
  2. _真的是私有的(其他类无法访问)还是仅仅是命名约定?
  1. Does it also apply to Flutter? Noting that there is no public/protected in Flutter.
  2. Will the _ really be private (inaccessible by other classes) or is it just a naming convention?

变量

 class RandomWordsState extends State<RandomWords> {
  final List<WordPair> _suggestions = <WordPair>[];
  final Set<WordPair> _saved = new Set<WordPair>();
  final TextStyle _biggerFont = const TextStyle(fontSize: 18.0);
  ...
}

  1. _是否也将小部件设为私有?在这种情况下,主类不会无法评估Widget吗?
  1. Does the _ make the Widget private too? In this case, wouldn't the main class be unable to assess the Widget?

小部件

Widget _buildRow(WordPair pair) {
  final bool alreadySaved = _saved.contains(pair);  // Add this line.
  ...
}

推荐答案

只是命名约定.下划线字段,类和方法仅在定义它们的.dart文件中可用.

It's not just a naming convention. Underscore fields, classes and methods will only be available in the .dart file where they are defined.

通常的做法是将窗口小部件的State实现私有化,从而使其只能由相应的StatefulWidget实例化:

It is common practice to make the State implementation of a widget private, so that it can only be instantiated by the corresponding StatefulWidget:

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

这篇关于下划线"_"是什么意思?在变量名称之前表示Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆