Pass data from one screen to another in Flutter with Firestore

Asked

Viewed 1,303 times

0

I need a gigantic help, I’m having a hard time passing data from one screen to another on Flutter, I’m using Firebase with Firestore on a stream, but I don’t know what I put in Navigator, and how to make the variables for the second screen take the data from the first... Succor!

I need to pass on the data from Productlist to the Productscreen....

Github link if you want to analyze https://github.com/alexandrechoske/BigTudoStore_Flutter

Tetei perform like this but he is not recognizing on Productscreen..

class ProductScreen extends StatefulWidget {

  ProductScreen({this.ref, this.descr}); /* Esse é o creator que vai receber os dados */
  final String ref;
  final String descr;

  @override
  _ProductScreenState createState() => _ProductScreenState();
}

class _ProductScreenState extends State<ProductScreen> {


  @override
  Widget build(BuildContext context) {
    return Container(
      child: Card(
        child: ListTile(
          title: Text(descr)
        ),
      ),
    );
  }
}

and on onTap:

onTap: () {Navigator.push(context, MaterialPageRoute(builder: (context) => ProductScreen(ref: snapshot["ref"], descr: snapshot["descr"])));},

But it accuses an Undefined error name 'descr' on Productscreen.

Tks!!

  • What about beauty? Put in your question what you’ve done to try to pass the data between the screens, it’s just you edit your question and ask... Don’t just use links, because one day you can take your source from there!

  • Sorry...but I’ve tried so many things that I wouldn’t even know how to describe here hehehe so I thought posting the code would be more relevant to the situation....

  • Yes, the code is relevant, but put an example as in my answer, so your question will not be closed and we will be able to help you better.

1 answer

1


As you have not put what you have already tried in your question, I will make an example medium above so that you understand.

Modify your Productscreen class as follows:

If you use the Statelesswidget

class ProductScreen statelessWidget{
  ProductScreen({this.nome, this.preco}); /* Esse é o creator que vai receber os dados */
  final String nome;
  final double preco;

  /* Substituir aqui com a sua programação normal*/
  Widget build(BuildCOntext context){
    return Coloumn(
      children: <Widget> [
        Text(nome),
        Text(preco.toString())
      ]
    )
  }
}

If you use the Statefulwidget

class ProductScreen extends StatefulWidget {

  ProductScreen({this.ref, this.descr}); /* Esse é o creator que vai receber os dados */
  final String ref;
  final String descr;

  @override
  _ProductScreenState createState() => _ProductScreenState();
}

class _ProductScreenState extends State<ProductScreen>{
  /* Substituir aqui com a sua programação normal*/
  Widget build(BuildCOntext context){
    return Coloumn(
      children: <Widget> [
        Text(widget.nome),
        Text(widget.preco.toString())
      ]
    )
  }
}

In the onTap() from your list item, do so:

var navigation = await Navigator.push(context, MaterialPageRoute(builder: (context) => ProductScreen(nome: document["nome"], preco: document["preco"])));

Since apparently you are starting with Flutter, I recommend you study some basic language things first!

You can read a little more here Send data to a new screen.

  • Tks Matheus, I’m taking a test like this! As I remember this way I had not tested, I had done something similar but he presented an error when putting in Navigator with []. "The Operator '[]' isnt defined for the class 'Querysnapshot'. Try Defining the operator '[]'.

  • That’s the point I’m referring to for you to edit your question and put something you already tried, so we can help you better.

  • @Choske read my comment, I advised you EDIT your question, it is not cool to put code here in the comments, if you take a look just below your question you will find a text edit. (You can edit by the link I left here tbm in the comment)

  • 1

    I edited it.... but it seems that it was a bit buggy here in the stack at the time that I pasted the code there :(

  • @Choske check my respsota again, made some adjustments, apply for your case.

  • 1

    able that was only put this widget :(...thanks Matheus, sorry for the ignorance, I added you on Linkedin...thanks really :)

Show 1 more comment

Browser other questions tagged

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