getter'text'在null上被调用

时间:2019-11-17 10:10:20

标签: flutter flutter-layout

我正在尝试从弹出的alertDialog中获取用户密码。用户在具有passController作为TextEditingController的文本字段上键入密码,并通过调用passController.text收集文本,但是发生了错误。

  void showDialog1(String msg, String jsonString) {

    TextEditingController passController;

    // flutter defined function
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Please Enter Password"),
          content: TextField(
              decoration: const InputDecoration(
                labelText: 'Password *',
              ),
              obscureText: true,
              controller: passController),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            new FlatButton(
              child: new Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
                submit(jsonString, passController.text);
              },
            ),
          ],
        );
      },
    );
  }

这是响应的调试控制台。

I/flutter (12758): #6      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
I/flutter (12758): #7      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
I/flutter (12758): #8      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
I/flutter (12758): #9      PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
I/flutter (12758): #10     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
I/flutter (12758): #11     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
I/flutter (12758): #12     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
I/flutter (12758): #13     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
I/flutter (12758): #14     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
I/flutter (12758): #18     _invoke1 (dart:ui/hooks.dart:263:10)
I/flutter (12758): #19     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
I/flutter (12758): (elided 3 frames from package dart:async)
I/flutter (12758):
I/flutter (12758): Handler: "onTap"
I/flutter (12758): Recognizer:
I/flutter (12758):   TapGestureRecognizer#f307f
I/flutter (12758): ════════════════════════════════════════════════════════════════════════════════════════════════════

1 个答案:

答案 0 :(得分:0)

由于仅创建变量,因此它将始终为null。请尝试以下方法:

TextEditingController passController = new TextEditingController();