当我在Flutter中使用 TextField 小部件并将属性 autoFocus 的值设置为 true
时import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: TextField(
autofocus: true,
),
),
);
}
}
flutter抛出错误
I/flutter (31721): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
I/flutter (31721): The following assertion was thrown while dispatching notifications for FocusNode:
I/flutter (31721): RenderBox was not laid out: RenderEditable#bf516 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (31721): 'package:flutter/src/rendering/box.dart':
I/flutter (31721): Failed assertion: line 1687 pos 12: 'hasSize'
I/flutter (31721):
I/flutter (31721): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (31721): more information in this error message to help you determine and fix the underlying cause.
I/flutter (31721): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (31721): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (31721):
I/flutter (31721): When the exception was thrown, this was the stack:
I/flutter (31721): #2 RenderBox.size
package:flutter/…/rendering/box.dart:1687
I/flutter (31721): #3 EditableTextState._updateSizeAndTransform
package:flutter/…/widgets/editable_text.dart:1729
I/flutter (31721): #4 EditableTextState._openInputConnection
package:flutter/…/widgets/editable_text.dart:1415
I/flutter (31721): #5 EditableTextState._openOrCloseInputConnectionIfNeeded
package:flutter/…/widgets/editable_text.dart:1441
I/flutter (31721): #6 EditableTextState._handleFocusChanged
package:flutter/…/widgets/editable_text.dart:1707
I/flutter (31721): #7 ChangeNotifier.notifyListeners
package:flutter/…/foundation/change_notifier.dart:206
I/flutter (31721): #8 FocusNode._notify
package:flutter/…/widgets/focus_manager.dart:808
I/flutter (31721): #9 FocusManager._applyFocusChange
package:flutter/…/widgets/focus_manager.dart:1401
I/flutter (31721): (elided 12 frames from class _AssertionError and package dart:async)
I/flutter (31721):
I/flutter (31721): The FocusNode sending notification was:
I/flutter (31721): FocusNode#ce6fe
I/flutter (31721): ════════════════════════════════════════════════════════════════════════════════════════════════════
为什么,如何解决这个问题。
我扑扑的版本:
[√] Flutter(频道稳定,v1.12.13 + hotfix.5,在Microsoft Windows上[版本10.0.18362.535],语言环境en-US)[√] Android工具链-为Android设备开发(Android SDK版本29.0)。 2)[√] Android Studio(3.5版)[√] VS代码(1.41.0版)[√]已连接设备(1个可用)•未发现问题!
================================================ =======================
答案 0 :(得分:1)
即使在官方示例https://flutter.dev/docs/cookbook/forms/focus中,也存在此问题
因为有了自动聚焦,键盘才会首先显示,但文本字段仍然不存在
解决方法是使用FocusNode
和WidgetsBinding.instance.addPostFrameCallback
显示TextField并将焦点移到此TextField时
代码段
@override
void initState(){
super.initState();
myFocusNode = FocusNode();
WidgetsBinding.instance.addPostFrameCallback((_){
FocusScope.of(context).requestFocus(myFocusNode);
});
}
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FocusNode myFocusNode;
@override
void initState(){
super.initState();
myFocusNode = FocusNode();
WidgetsBinding.instance.addPostFrameCallback((_){
FocusScope.of(context).requestFocus(myFocusNode);
});
}
@override
void dispose() {
// Clean up the focus node when the Form is disposed.
myFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: TextField(
//autofocus: true,
focusNode: myFocusNode,
),
),
);
}
}
答案 1 :(得分:1)
此错误已得到修复,但目前尚未在稳定的通道(https://github.com/flutter/flutter/issues/52221)中修复。 如果您的项目允许,则可以切换到dev通道(> 1.15)并仅使用自动对焦。
从命令行:
flutter channel dev
flutter upgrade
重建您的项目,它应该可以工作。
答案 2 :(得分:0)
在2020年,即使在开发者通道中也仍然会发生此错误...
我不好,它正在工作。我的变量为null并收到错误,对不起