dart - 我尝试使用 TextField 的 Controller ,但我收到错误 "NoSuchMethodError: The method ' 调用' was called on null"

标签 dart flutter

我尝试使用 TextField 的 Controller ,但我收到错误

"NoSuchMethodError: The method 'call' was called on null"

如果我使用 onChange() 就可以了。

我的代码:

class _MyHomePageState extends State<MyHomePage> {
  Icon _searchIcon = Icon(Icons.search, color: Colors.white);
  int _searchIconState = 0;
  Widget _appBarTitle;
  TextEditingController _controller = TextEditingController();

  _onChange() {
    String text = _controller.text;
    print(text);
  }

  @override
  void initState() {
    super.initState();

   /* My TextField */
    _appBarTitle = TextField(
        controller: _controller,
        onChanged: (text) {
          print('onChanged: ' + text);
        },
        style: TextStyle(color: Colors.white, fontSize: 18),
        decoration: InputDecoration(
            border: InputBorder.none,
            icon: _searchIcon,
            hintText: 'Search...',
            hintStyle:
            TextStyle(color: Colors.white.withOpacity(0.5), fontSize: 18)));

    _controller.addListener(_onChange());
  }

  @override
  void dispose() {
    // Clean up the controller when the Widget is removed from the Widget tree
    // This also removes the _printLatestValue listener
    _controller.dispose();
    super.dispose();
  }

  _nestedScrollViewController() {}

  _tabBarController() {}

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: NestedScrollView(
          controller: _nestedScrollViewController(),
          headerSliverBuilder: (BuildContext context, bool isScrolled) {
            return <Widget>[
              SliverAppBar(
                title: _appBarTitle /* TextField put in here */,             
                pinned: true,
                floating: true,
                forceElevated: isScrolled,
                bottom: TabBar(
                  tabs: <Widget>[
                    Tab(text: 'TO DAY'),
                    Tab(text: 'TOMORROW'),
                    Tab(text: '7Days')
                  ],
                  controller: _tabBarController(),
                ),
              )
            ];
          },
          body: Scaffold(
            body: TabBarView(
              children: <Widget>[
                todayUI(),
                tomorrowUI(),
                weekUI(),
              ],
            ),
          )
        ),
      ),
    );
  }
}

最佳答案

你添加监听器的方式不正确

你必须在 onChange 之后移除 ()

_controller.addListener(_onChange());    

_controller.addListener(_onChange);     

关于dart - 我尝试使用 TextField 的 Controller ,但我收到错误 "NoSuchMethodError: The method ' 调用' was called on null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54995056/

相关文章:

Dart : How to ignore omit_local_variable_types warning?

if-statement - 如何在9、109和209等处执行其他操作,在99、199和299等处进行其他操作,以及对以9结尾的所有其他操作执行一系列其他操作?

android - Flutter 示例应用程序无法正常工作

flutter - 启动 flutter 应用程序时,我在哪里运行初始化代码?

android - 将自定义 boxshadow 添加到 Flutter 卡

flutter - 我必须为每个文本字段分配一个 TextEditingController 吗? flutter

dart - 带有聚合物运行时错误的Dart2Js

objective-c - 如何使用 Dart ffi 表示具有 int 和字符串声明的 ObjC 枚举 AVAudioSessionPortOverride?

dart - 创建从上到下的滑动效果

flutter : Why can't I move the cursor in the TextField to the beginning of the text?