我想在flutter中获取输入到TextField()中的int数据,我使用TextEditingController是这样的:
TextEditingController _section_id = new TextEditingController();
并在其中使用此控制器:
TextField(controller: _section_id,
keyboardType: TextInputType.number,)
我以这种方式获取int数据:
Repository().placeAddApiProvider(int.parse(_section_id.text));
但显示错误:
E/flutter (25586): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Invalid number (at character 1)
E/flutter (25586):
E/flutter (25586): ^
E/flutter (25586):
E/flutter (25586): #0 int._throwFormatException (dart:core/runtime/libintegers_patch.dart:129:7)
E/flutter (25586): #1 int.parse (dart:core/runtime/libintegers_patch.dart:53:14)
E/flutter (25586): #2 AddPlaceState.build.<anonymous closure> (package:mosul/src/ui/users/add_place.dart:93:37)
E/flutter (25586): <asynchronous suspension>
E/flutter (25586): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
E/flutter (25586): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
E/flutter (25586): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
E/flutter (25586): #6 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (25586): #7 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter (25586): #8 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (25586): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:20)
E/flutter (25586): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
E/flutter (25586): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
E/flutter (25586): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (25586): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter (25586): #14 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (25586): #15 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (25586): #16 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (25586): #17 _invoke1 (dart:ui/hooks.dart:223:10)
E/flutter (25586): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
E/flutter (25586):
V/ActivityThread(25586): updateVisibility : ActivityRecord{1e7c9680 token=android.os.BinderProxy@2565abb2 {org.codeforiraq.mosul/org.codeforiraq.mosul.MainActivity}} show : false
E/OpenGLRenderer(25586): SFEffectCache:clear(), mSize = 0
W/IInputConnectionWrapper(25586): showStatusIcon on inactive InputConnection
在(Repository()。placeAddApiProvider)中没有问题,并且从section_id的API返回的数据为int,我尝试邮递员,并且一切正常吗? 谢谢
答案 0 :(得分:0)
您的TextField
的初始值只是一个空字符串。尝试将其解析为int
会产生FormatException
错误。
您可能想像这样重写代码:
if (_section_id.text.trim() != "") {
Repository().placeAddApiProvider(int.parse(_section_id.text.trim()));
}