错误:期望值为'TextEditingController'类型的值,但得到了'TextEditingValue'类型的值

时间:2020-08-19 06:31:41

标签: flutter dart

我正在尝试将ValueListenableBuilder包裹在Textfield上,该字段具有接受输入文本并返回相同文本的功能。最初的目的是通过数据库保留输入数据。但是在实现下面给出的基本代码时,我得到了错误“预期类型为'TextEditingController'的值,但类型为'TextEditingValue'的一个”。您能启发我这个错误吗?

import 'package:flutter/material.dart';

void main() => runApp(MyTextFieldApp());

class MyTextFieldApp extends StatelessWidget {
  final _controller = TextEditingController();
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        body: Container(
          padding: EdgeInsets.all(24.0),
          child: Center(
            child: ValueListenableBuilder(
                valueListenable: _controller,
                builder: (BuildContext context, _controller, _ ) {
                   return TextField(
                     autofocus: true,
                     maxLines: 6,
                     controller: _controller,
                     decoration: InputDecoration(
                      labelText: "Note",
                       border: OutlineInputBorder(
                       borderRadius: BorderRadius.circular(5.0),
                     ),
                    ),
                   );
                },
             ),
          )
        )
      )
    );
  }
}

2 个答案:

答案 0 :(得分:0)

您没有将TextEditingController传递给TextField

ValueListenableBuilder(
                valueListenable: _controller,
                builder: (BuildContext context, _controller, _ ) {
// this _controller is not equal to the valueListenable: _controller above, it means _controller.value
                   return TextField(
                     autofocus: true,
                     maxLines: 6,
                     controller: _controller,
                     decoration: InputDecoration(
                      labelText: "Note",
                       border: OutlineInputBorder(
                       borderRadius: BorderRadius.circular(5.0),
                     ),
                    ),
                   );
                },
             ),
ValueListenableBuilder(
                valueListenable: _controller,
                builder: (BuildContext context, _value, _ ) {
                   return TextField(
                     autofocus: true,
                     maxLines: 6,
                     controller: _controller,// assign the TextEditingController
                     decoration: InputDecoration(
                      labelText: "Note",
                       border: OutlineInputBorder(
                       borderRadius: BorderRadius.circular(5.0),
                     ),
                    ),
                   );
                },
             ),

答案 1 :(得分:0)

您应该执行以下操作:

Private Sub CommandButton1_Click()
  'Set Form
    UserForm1.Caption = "Test"
  'Create Form-Elements (Commandbutton)
    Dim cmdButton01 As MSForms.CommandButton
    Set cmdButton01 = UserForm1.Controls.Add("Forms.CommandButton.1", "dynCmdButton01")
    cmdButton01.Width = 50
    cmdButton01.Caption = "Save"
  'Show Form
    UserForm1.Show
  End Sub

Private Sub dynCmdButton01_click()
  MsgBox "Test"
  End Sub

从文档中

import 'package:flutter/material.dart'; void main() => runApp(MyTextFieldApp()); class MyTextFieldApp extends StatelessWidget { final _controller = TextEditingController(); final ValueNotifier valueNotifier = ValueNotifier("initial"); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title : Text("Title")), backgroundColor: Colors.white, body: Container( padding: EdgeInsets.all(24.0), child: Center( child: ValueListenableBuilder( valueListenable: valueNotifier, builder: (BuildContext context, values, child ) { return Column( children : <Widget>[ TextField( autofocus: true, maxLines: 6, controller: _controller, decoration: InputDecoration( labelText: "Note", border: OutlineInputBorder( borderRadius: BorderRadius.circular(5.0), ), ), ), RaisedButton(child : Text("click me"),onPressed : (){ valueNotifier.value = _controller.text; }), Text(values), ], ); }, ), ) ) ) ); } }

其内容与ValueListenable保持同步的小部件。

给出一个ValueListenable和一个根据T的具体值构建窗口小部件的构建器,此类将自动将自己注册为ValueListenable的侦听器,并在值更改时使用更新后的值调用该构建器。

ValueListenableBuilder<T>属性的类型为valueListenable,它是由ValueListenable<T>实现的接口。

因此,您需要创建ValueNotifier<T>的实例:

ValueNotifier<T>

在这种情况下,我使用类型final ValueNotifier valueNotifier = ValueNotifier("initial"); 和初始值String创建了它。然后将此实例分配给属性initial

valueListenable

valueListenable: valueNotifier, 类型的builder仅在更新valueNotifier时被调用。

因此,您可以创建RaisedButtononPressed,可以更新valueNotifier值,该值将调用builder并更新Text小部件。

ValueWidgetBuilder<T>