Flutter passing data between screens

Asked

Viewed 2,975 times

0

I have a doubt that I believe to be simple but as I have never done so I am struggling. I want to pass the data of a contact to another screen of the app.

Where I call an Actionsheet that appears the option to edit the comment of the contact (which redirects to the other screen). I believe the error is to pass the necessary contact parameters through the button, but as ?

Canvas of the Actionsheet:

CupertinoButton(
                padding: EdgeInsets.zero,
                child: const Icon(
                  CupertinoIcons.pen,
                  semanticLabel: 'Editar',
                ),
                onPressed: () {
                  showCupertinoModalPopup(
                      context: context,
                      builder: (context) {
                        //É necessário retornar aqui o actionsheet para pegar os
                        //dados do contato
                        return CupertinoActionSheet(
                          title: Text(model.contactsList[index]["fullname"]),
                          message:
                          Text(model.contactsList[index]["comentario"]),
                          cancelButton: CupertinoActionSheetAction(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: Text("Sair"),
                          ),
                          //Itens do action sheet
                          actions: <Widget>[
                            CupertinoActionSheetAction(
                              onPressed: () {
                            },
                              child: CupertinoButton(
                                child: Text("Editar comentario"),
                                onPressed: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) =>
                                              UpdateComent()));
                                },
                              ),
                            ),
                          ],
                        );
                      });
                },
              ),

Here is my screen where I intend to update, however, accuses the error of being receiving null

The getter 'contactsList' was called on null.
Receiver: null
Tried calling: contactsList
User-created ancestor of the error-causing widget was: 
Container file:///home/marcelo/%C3%81rea%20de%20Trabalho/vp%20App/aplicativo-vpeventos/lib/update.dart:48:19`

And when you click this "link" points to the Container(child: CommentStyle();)

In the second block.


class UpdateComent extends StatefulWidget {
  @override
  _UpdateComentState createState() => _UpdateComentState();
}

class _UpdateComentState extends State<UpdateComent> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CupertinoNavigationBar(
          transitionBetweenRoutes: false,
          trailing: ScopedModel<UserModel>(
              model: UserModel(),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const Padding(padding: EdgeInsets.only(left: 8.0)),
                  CupertinoButton(
                      padding: EdgeInsets.zero,
                      child: const Tooltip(
                        message: 'Back',
                        child: Text('Salvar comentario'),
                        excludeFromSemantics: true,
                      ),
                      onPressed: () {

                      }),
                ],
              )),
        ),

        //Corpo da tela
        body: ScopedModelDescendant<UserModel>(builder: (context, child, model){
              return SingleChildScrollView(
                child:
                  Container(
                    child: CommentStyle(
                    ),
                  ),

              );

        }));
  }
}

//Aqui monto o corpo da tela 
class CommentStyle extends StatelessWidget {
 const CommentStyle({this.index, this.model});


 final int index;
 final UserModel model;

  @override
  Widget build(BuildContext context) {
    //printei o index e ele retorna null
    print("print aqui");
    print(index);

    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(15.0),
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    StyleText("Nome:"),
                   Text(model.contactsList[index]["fullname"]),
                  ],
                ),
                Row(
                  children: <Widget>[
                    StyleText("Email:"),
                    Text(model.contactsList[index]["email"]),
                  ],
                ),
                StyleText("Edite o comentario do usuario no campo abaixo"),
                CupertinoTextField(
                  controller: _controllerComent,
                  maxLines: 7,
                  decoration: BoxDecoration(
                      border: Border.all(
                          width: 1.0,
                          color: Colors.grey
                      ),
                      borderRadius: BorderRadius.circular(15)
                  ),
                  placeholder: model.contactsList[index]["comentario"],
                ),
              ],
            )
          ),

        ],
      ),

    );
  }
}
  • What error occurs? At what point does the error appear? Tell us the complete error, say that "is receiving NULL is very generic"

  • I edited the question, I believe that this way it becomes more clear

  • Yes, it was more detailed, check my reply that I explain to you how to solve.

1 answer

1


I believe your problem may be here:

    body: ScopedModelDescendant<UserModel>(builder: (context, child, model){
          return SingleChildScrollView(
            child:
              Container(
                child: CommentStyle(
                ),
              ),

          );

    }));

You are creating the CommentStyle without passing the parameters...

Modifications

In the ActionSheet change the button to

CupertinoButton(
  child: Text("Editar comentario"),
  onPressed: () {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>
                UpdateComent(index)));
  },
),

Modify your class UpdateComent to receive the INDEX parameter

class UpdateComent extends StatefulWidget {
  UpdateComent({this.index});
  final Integer index;
  @override
  _UpdateComentState createState() => _UpdateComentState();
}

class _UpdateComentState extends State<UpdateComent> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CupertinoNavigationBar(
        transitionBetweenRoutes: false,
        trailing: ScopedModel<UserModel>(
          model: UserModel(),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              const Padding(padding: EdgeInsets.only(left: 8.0)),
              CupertinoButton(
                padding: EdgeInsets.zero,
                child: const Tooltip(
                  message: 'Back',
                  child: Text('Salvar comentario'),
                  excludeFromSemantics: true,
                ),
                onPressed: () {

                }
              ),
            ],
          )
        ),
      ),
      body: ScopedModelDescendant<UserModel>(builder: (context, child, model){
            return SingleChildScrollView(
              child:
                Container(
                  child: CommentStyle(widget.index, model),
                ),

            );

      }));
  }
}

Okay, now your widget CommentStyle is receiving the proper parameters when created.

Explanation

How you are creating the Widget CommentStyle without passing the necessary parameters, when being created it assigns NULL to the properties. So when trying to give the Print(index) it will return NULL.

  • 1

    ScopedModel determines some database-related functions. Briefly, it’s like a user session.

  • @Marcelol check my reply again, I made some adjustments to make it more detailed.

  • 1

    It worked here @Matheus Ribeiro. Thanks for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.