gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:53:43 25 4
gpt4 key购买 nike

我尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com