【问题标题】:Return multiple data from a TextField flutter从 TextField 颤振中返回多个数据
【发布时间】:2020-12-23 21:20:48
【问题描述】:

我想从另一个屏幕返回两个文本字段的内容,在包含标题和文本的卡片中,在 ListView 中。基本上在主页中,当我点击添加按钮时,它会将我带到另一个页面,其中有两个文本字段,一个用于文本,一个用于标题,当我点击一个按钮时,我想在我的列表视图中创建一张新卡片包含两个文本字段内容的主页。我尝试了一些方法,但出现了这个错误:

type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String'

这是主页的代码:

class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
static List dreams = [];
@override
Widget build(BuildContext context) {
 return Scaffold(
  backgroundColor: kEdgeColor,
  appBar: AppBar(
    backgroundColor: kEdgeColor,
    elevation: 0,
    title: Text('My dreams'),
  ),
  body: Container(
    decoration: BoxDecoration(
        color: Colors.black),
    child: ListView.builder(
        itemCount: dreams.length ,
        itemBuilder: (BuildContext ctxt, int index) {
          return new DreamCard(
            Content: dreams[index],
            title:  dreams[index+1],
          );
        }
    )
  ),

  bottomNavigationBar: BottomAppBar(
    color: kEdgeColor,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [

        FlatButton(
          onPressed: (){
            Navigator.popAndPushNamed(context, '/public');
          },
          child: Icon(Icons.public),
          color: Colors.black,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
        ),

        FlatButton(
          onPressed: (){
            Navigator.popAndPushNamed(context, '/');
          },
          child: Icon(Icons.bedtime),
          color: Colors.black,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
        ),
      ],
    ),
  ),
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  floatingActionButton: FloatingActionButton(
    onPressed: () async {
      final result = await Navigator.of(context).push(MaterialPageRoute(builder: (context)    {return WritingPage();}));
      if (null != result) {
        dreams.add(result);
        setState(() {});
      }
    },
    child: Icon(Icons.add, size: 30,color: Colors.black,),
    backgroundColor: Colors.white,


  ),
);

} }

现在这是在写作页面上按下按钮时调用的函数:

void submit() {
Navigator.of(context).pop({
"text": _textEditingController.text.trim(),
"title": _titleEditingController.text.trim(),
});
}

最后这是我想在列表视图中显示的卡片的构造函数:

DreamCard({this.Content, this.title});

final String title;
final String Content;

如果你有任何想法,请告诉我!

【问题讨论】:

    标签: flutter dart navigator


    【解决方案1】:

    请尝试以下代码:

    确保导入 'dart:convert';

    import 'dart:convert';
    

    .....

    void submit() {
      Navigator.of(context).pop(jsonEncode({
        "text": _textEditingController.text.trim(),
        "title": _titleEditingController.text.trim(),
      }));
    }
    

    ....

    DreamCard 中的再次使用 jsonDecode 来解码你收到的字符串。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2020-09-03
      • 2020-04-13
      • 2021-09-23
      • 2019-04-04
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多