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

问题描述 投票:0回答:1

所以基本上我是飞镖和飞镖的新手。问题是当我向用户询问url输入,然后从输入链接中获取OpenGraph数据,并试图显示一个带有TextFormFields的新列时,它具有从URL的打开图中得到的initialValu的initialValu

我尝试使用新数据设置TextFormField controller.text,还尝试设置当用户按下“提交”按钮时设置的变量,但我不断收到有关此错误的信息

════════════════════════════════════════════════════════════════════════════════
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'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

当您过去链接时,我想在Facebook上做类似的事情,它会立即对该链接进行预览,但是我需要用户能够在加载数据后对其进行编辑]]

所以基本上我是飞镖和飞镖的新手。问题是当我向用户询问url输入,然后从输入链接中获取OpenGraph数据并尝试使用...

flutter flutter-layout open-graph-protocol
1个回答
0
投票

错误是因为不能同时将initialValuecontroller设置为TextFormField,是一个或另一个。

© www.soinside.com 2019 - 2024. All rights reserved.