Search code examples
flutterdartnavigator

Return multiple data from a TextField flutter


I want to return the content of two textfields from another screen, in a card which contain a title and a text, in a ListView. Basically in the home page when I hit the add button it brings me to another page where there are two textfields, one for text and one for title, and when I hit a button, I want to create a new card in the listview of my home page with the content of the two textfields. I tried something but I got this error :

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

Here's the code of the home page :

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,


  ),
);

} }

Now this is the function which is called when the button is pressed on the writing page :

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

And finally this is the constructor of my card I want to display in my listview :

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

final String title;
final String Content;

If you have any idea please tell me!


Solution

  • Please try the following code :

    Make sure to import 'dart:convert';

    import 'dart:convert';
    

    .....

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

    ....

    The again in DreamCard use jsonDecode to decode the String you received.