skip to Main Content

I’m new to flutter so bear with me. Currently I’m passing data from one ui to another. In the first UI I’m passing like this,

 await Navigator.push(context, MaterialPageRoute(
        builder: (context) {
          return ItemCategory(itemDocId: returnDocID.toString());
        },
      ));

In the 2nd UI I’m trying to receive that data. Yes I know you can do it by ‘widget.’. But I need to use it before the builder. So the 2nd UI code part is like this.

lass ItemCategory extends StatefulWidget {
  // Item Id
  final String itemDocId;

  const ItemCategory({super.key, required this.itemDocId});

  @override
  State<ItemCategory> createState() => _ItemCategoryState(itemDocId);
}

class _ItemCategoryState extends State<ItemCategory> {

static final List<Widget> _widgetOptions = <Widget>[
    CatGrid1(itemDocId: widget.itemDocId),
    Text(
      'Index 1: Business',
    ),
    Text(
      'Index 2: School',
    ),
    Text(
      'Index 2: School',
    ),
  ];

as yiu can see on above coding I’m trying to send data to another ui called CatGrid1 . But here comes the problem. I’m trying to send that string I passed from 1st ui to this 1. And a error comes and saying I can’t use "widget.itemDocId". Which is the line "CatGrid1(itemDocId: widget.docId)"

And i fyou are wondering this list is for a drawer, but it’s not something that closes. It’s static. So when answering, I’m not expecting that.

Currently I tried passing with and without "widget." , But no luck getting what I want.

And as I’m new to flutter, I would really appreciate getting a custom code from someone rather than marking as a duplicate

2

Answers


  1. as I remember you can not directly assign class property to another variable , what you can do is you can store the itemDocId in new variable by like this:

    late String newItemDocId;
    @override
    initState{
    super.initState();
    newItemDocId = widget.itemDocId;
    }
    

    then you can use the new variable as you want !

    Login or Signup to reply.
  2. You have to implement build function in ItemCategory, like this:

    class ItemCategory extends StatefulWidget {
      // Item Id
      final String itemDocId;
    
      const ItemCategory({super.key, required this.itemDocId});
    
      @override
      State<ItemCategory> createState() => _ItemCategoryState(itemDocId);
    }
    
    class _ItemCategoryState extends State<ItemCategory> {
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
        body: Column(
        children:[
        CatGrid1(itemDocId: widget.itemDocId),
        Text(
          'Index 1: Business',
        ),
        Text(
          'Index 2: School',
        ),
        Text(
          'Index 2: School',
        )]);
      
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search