尝试从用户 url 输入中获取一些数据,然后显示 TextFormField 并使用获取的数据对其进行初始化

trying to fetch some data from user url input, before reveal TextFormField and initial them with the fetched data

所以基本上我是 dart 和 flutter 的新手。 问题是当我向用户询问 url 输入,然后我从输入 link 中获取 OpenGraph 数据,并尝试用 TextFormFields 显示一个新的 Column 以及我从 [=25] 得到的 initialValu =]的开图

我尝试用新数据设置 TextFormField controller.text,还尝试设置一个 var,当用户按下提交按钮时我设置了它,但我一直收到有关它的错误

════════════════════════════════════════════════════════════════════════════════
Reloaded 5 of 635 libraries in 762ms.

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building EditPostScreen(dirty, state: _EditPostScreenState#f6ac9):
'package:flutter/src/material/text_form_field.dart': Failed assertion: line 117 pos 15: 'initialValue == null || controller == null': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

User-created ancestor of the error-causing widget was
    MaterialApp 
lib\main.dart:25
When the exception was thrown, this was the stack
#2      new TextFormField 
package:flutter/…/material/text_form_field.dart:117
#3      _EditPostScreenState.build 
package:crusty/…/edit_post/edit_post_screen.dart:57
#4      StatefulElement.build 
package:flutter/…/widgets/framework.dart:4047
#5      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:3941
#6      Element.rebuild 
package:flutter/…/widgets/framework.dart:3738
...
════════════════════════════════════════════════════════════════════════════════
Reloaded 5 of 635 libraries in 700ms.

那是我所有的小部件

  final _formKey = GlobalKey<FormState>();
  final _urlController = TextEditingController();
  final _titleController = TextEditingController();
  final _imageController = TextEditingController();


  var _isFetchedUrl = false;

  @override
  void dispose() {
    _urlController.dispose();
    _imageController.dispose();
    _titleController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(''),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            TextFormField(
              controller: _urlController,
              autofocus: true,
              decoration:
                  InputDecoration(hintText: 'Enter the url for your post'),
              validator: (value) {
                bool _validURL = Uri.parse(value).isAbsolute;
                if (value.isEmpty) {
                  return 'Please enter url for your new post';
                } else if (_validURL) {
                  return 'Pleas make sure you entered valid URL';
                }
                return null;
              },
            ),
            _isFetchedUrl == false
                ? Container()
                : Column(
                    children: <Widget>[
                      TextFormField(
                          controller: _titleController,
                          initialValue: _titleController.text,
                          decoration: InputDecoration(
                              hintText: 'Enter title for your post'),
                          validator: (value) {
                            if (value.isEmpty) {
                              return 'Please enter title for your new post';
                            }
                            return null;
                          }),
                      TextFormField(
                          controller: _imageController,
                          initialValue: _imageController.text,
                          decoration: InputDecoration(
                              hintText: 'Enter subtitle for your post'),
                          validator: (value) {
                            if (value.isEmpty) {
                              return 'Please enter subtitle for your new post';
                            }
                            return null;
                          }),
                    ],
                  ),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              child: RaisedButton(
                onPressed: () async {
                  // Validate returns true if the form is valid, or false
                  // otherwise.
                  if (_formKey.currentState.validate()) {
                    // If the form is valid, display a Snackbar.
                    Scaffold.of(context).showSnackBar(
                        SnackBar(content: Text('Processing Data')));
                  }
                  var data = await OpenGraphParser.getOpenGraphData(_urlController.text);
                  print(data['description']);

                  setState(() {
                    _titleController.text = data['description'];
                   _imageController.text = data['image'];
                   _isFetchedUrl = true;
                  });
                  // print(_urlController.text);
                },
                child: Text('Load my post'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

我想在你通过 link 时在 Facebook 中做一些类似的事情,它会立即预览那个 link 但我需要用户在我加载后编辑它的能力数据

错误是因为你不能同时将initialValuecontroller设置为TextFormField,是其中之一。

如果您想使用控制器设置初始值,那么当您初始化控制器时,您可以将初始值传递给它,如下所示:

final _urlController = TextEditingController(text: "Initial value");